Need help with my app.

Making an app for computer class. A random number will be generated and the base ten blocks showing how much the. Number is worth show up. My game is working you can test it by changing the sprites but , when it’s the right answer I’ve told it to print yes, and when it’s wrong print no, and if it’s right generate another number to do. After at least one question has been completed, whenever the right answer is on say the second button, it prints yes but it also prints no. I have the actual game with levels and a game over screen, and when you press the right answer it goes to game over even though it still prints yes.
Is there something wrong with my code?

local buttonExample
local buttonExample2
local buttonExample3
level = 1

function setup()
    t=60*60
    displayMode(FULLSCREEN)
    sprite("Dropbox:Generic Button PressedGOOD")
      buttonExample = NewButton("Dropbox:generic button copyGOOD","Dropbox:Generic Button PressedGOOD",vec2(800,200))
    buttonExample2 = NewButton("Dropbox:generic button copyGOOD","Dropbox:Generic Button PressedGOOD",vec2(800,400))
    buttonExample3 = NewButton("Dropbox:generic button copyGOOD","Dropbox:Generic Button PressedGOOD",vec2(800,600))
    answer=math.random(10,299)
    wrongAnswerOne=math.random(answer-20,answer+20)
    wrongAnswerTwo=math.random(answer-20,answer+20)
    answerLocation = math.random(1,3)
   displayMode(FULLSCREEN)
end

function draw()
      t=t-1
    if t==0 then
        print("you lose")
    end
    background(57, 203, 34, 255)
    local hundredsDivision = answer/100
    local hundreds = math.modf(hundredsDivision)
    local hundredsRemainder = answer - (hundreds*100)

    local tensDivision = hundredsRemainder/10
    local tens = math.modf(tensDivision)
    local ones = hundredsRemainder - (tens*10)
    
   
     

   -- Do your drawing here

   -- draw the hundreds
   for drawHundreds = 1, hundreds do
       sprite("Dropbox:base ten", (0 + drawHundreds*300), 530)
   end

   -- draw the tens
   for drawTens = 1, tens do
       sprite("Dropbox:onebyten", (47 + drawTens*70), 270)
   end
  -- draw the ones
   for drawOnes = 1, ones do
       sprite("Dropbox:single block", (47 + drawOnes*70), 110)
   end

    -- This sets a dark background color
    fontSize(35)
    font("Copperplate-Bold")
    fill(0, 0, 0, 255)
    
    if answerLocation == 1 then 
        text(answer, 900,200)
        text(wrongAnswerOne, 900,600)
        text(wrongAnswerTwo,900,400) 
        
    end
    if answerLocation == 2 then
        text(answer,900,400)
        text(wrongAnswerOne,900,200)
        text(wrongAnswerTwo, 900,600)
    end
    if answerLocation==3 then 
        text(answer,900,600)
        text(wrongAnswerOne,900,400)
        text(wrongAnswerTwo,900,200)
    end
    -- Do your drawing here
    buttonExample:draw()
    buttonExample2:draw()
    buttonExample3:draw()
    text("ANSWERS", 890,700)
    text("Counter:"..t,WIDTH/2,700) 
end

-- This function gets called once every frame
function touched(touch)
    -- Do your drawing here

    buttonExample:touched(touch)
    buttonExample2:touched(touch)
    buttonExample3:touched(touch)
    if answerLocation==1 then
    if(buttonExample.selected == true) then
        print("yes!!")
        answer=math.random(10,299)
        answerLocation=math.random(1,3)
        end
    if(buttonExample2.selected==true) then 
        print("No")
        end
    if (buttonExample3.selected==true) then
        print("No")
        end
    end
    if answerLocation==3 then
    if(buttonExample3.selected == true) then
        print("Yes")
        answer=math.random(10,299)
        answerLocation = math.random(1,3)
        end 
    if(buttonExample.selected==true)then 
        print("no")
        end
    if(buttonExample2.selected==true)then
        print("no")
        end
    end
   if answerLocation==2 then
    if(buttonExample2.selected==true)then
        print("yes")
        answer = math.random(10,299)
        answerLocation=math.random(1,3)
        end
    if(buttonExample3.selected==true)then 
        print("no")
        end
    if(buttonExample.selected==true)then 
        print("no")
        end
    end
end

I used a button class my teacher made, I’ll post it as a second post.

Hmm I’ll try it

@Jmv38 you’re a genius. Life saver. Thank you!

@jessicGriffin i didnt study you code and goal in details, but something stroke me: when you do you test you use

    if answerLocation==1 then
        ....
        answerLocation=math.random(1,3)
        ....
    end
    if answerLocation==3 then
        ....
        answerLocation = math.random(1,3)
        ...
    end

so you test for answerLocation then you modify it then you test again???
This doesnt seem right. Nor mally you should use elseif construct:

    if answerLocation==1 then
        ....
        answerLocation=math.random(1,3)
        ....
    -- no end here <<<<<<<<<<<<<<<<<<<<<<<<<
    elseif answerLocation==3 then. --<<<<<<<<<<< here elseif
        ....
        answerLocation = math.random(1,3)
        ...
    end

Try this to see if it helps

Here’s the button class:

-- Created by: Mr Coxall
-- Created on: Nov 2013
-- Created for: ICS2O
--
-- This class simplifies the process of checking if a sprite button is pressed or not
-- Then you can ask the sprite if it is being touched or was ended.
-- Parameters to pass in:
--  1. Untouched button image name
--  2. Touched button image name
--  3. A vector2 that is the location of the button

NewButton = class()

function NewButton:init(buttonImageName, touchedButtonImageName, buttonPosition)
    -- you can accept and set parameters here

    self.buttonImageName = buttonImageName
    self.touchedButtonImageName = touchedButtonImageName
    self.currentImageName = self.buttonImageName
    self.buttonLocation = buttonPosition or vec2(512,315)
    self.buttonState = nil
    self.buttonImageSize = vec2(spriteSize(self.buttonImageName))
    self.selected = false
end

function NewButton:checkTouching(touchPosition)
    -- check if the touch events are located inside the button
    print(self.buttonImageName)

    if( (self.buttonLocation.x - self.buttonImageSize.x/2) < touchPosition.x and
        (self.buttonLocation.x + self.buttonImageSize.x/2) > touchPosition.x and
        (self.buttonLocation.y - self.buttonImageSize.y/2) < touchPosition.y and
        (self.buttonLocation.y + self.buttonImageSize.y/2) > touchPosition.y ) then
            return true
        else
            return false
        end
end

function NewButton:draw()
    -- Codea does not automatically call this method

    pushStyle()
    pushMatrix()
    noFill()
    noSmooth()
    noStroke()

    sprite(self.currentImageName, self.buttonLocation.x, self.buttonLocation.y)

    popMatrix()
    popStyle()
end

function NewButton:touched(touch)
    -- Codea does not automatically call this method

    -- local varaibles
    local currentTouchPosition = vec2(touch.x, touch.y)

    -- reset touching variable to false
    self.selected = false

    if (touch.state == BEGAN) then
         if( (self.buttonLocation.x - self.buttonImageSize.x/2) <
currentTouchPosition.x and
            (self.buttonLocation.x + self.buttonImageSize.x/2) >
currentTouchPosition.x and
            (self.buttonLocation.y - self.buttonImageSize.y/2) <
currentTouchPosition.y and
            (self.buttonLocation.y + self.buttonImageSize.y/2) >
currentTouchPosition.y ) then

            self.currentImageName = self.touchedButtonImageName
            --print("Now touching! - began")
        else
            self.currentImageName = self.buttonImageName
            --print("Not touching - began")
        end
    end

    if (touch.state == MOVING) then
        if( (self.buttonLocation.x - self.buttonImageSize.x/2) <
currentTouchPosition.x and
            (self.buttonLocation.x + self.buttonImageSize.x/2) >
currentTouchPosition.x and
            (self.buttonLocation.y - self.buttonImageSize.y/2) <
currentTouchPosition.y and
            (self.buttonLocation.y + self.buttonImageSize.y/2) >
currentTouchPosition.y ) then

            self.currentImageName = self.touchedButtonImageName
            --print("Now touching! - moving")
        else
            self.currentImageName = self.buttonImageName
            --print("Not touching - moving")
        end
    end

    if (touch.state == ENDED) then
        if( (self.buttonLocation.x - self.buttonImageSize.x/2) <
currentTouchPosition.x and
            (self.buttonLocation.x + self.buttonImageSize.x/2) >
currentTouchPosition.x and
            (self.buttonLocation.y - self.buttonImageSize.y/2) <
currentTouchPosition.y and
            (self.buttonLocation.y + self.buttonImageSize.y/2) >
currentTouchPosition.y ) then

            self.currentImageName = self.buttonImageName
            self.selected = true
            --print("Activated button")
        else
            self.currentImageName = self.buttonImageName
            --print("Not touching - ended")
        end
    end
end

@Jmv38 it’s working in the test file I have, but when I copy paste it it starts working differently and doing the wrong thing. Any idea why?

Where did you paste it? (What code?)

Never mind, I fixed it, thanks @SkyTheCoder (sorry for late response)