0

I am coding a trivia game. When the player gets the question correct, the askQuestion function returns True, and the playGame function (which calls upon the askQuestion function) is supposed to add 1 point to the score. However, the score never increases.

I tried debugging the code, and saw that the program reaches the 'if askQuestion == True' line, but then does not continue on to increment the score.

# play game
def playGame(questions, startTime, score):
    # initialize start time
    startTime = time.time()
    # set remaining time equal to 60 seconds
    remainingTime = AVAILABLE_TIME
    # initialize time remaining
    timeLeft = 100    
    # initialize score to zero
    score = 0
    # initiazlize question number to zero
    questionNumber = 0
    # run loop while time left
    while timeLeft > 0:
        # go into spinner function
        spinner(questions[questionNumber][0], score)
        # go into askQuestion function
        askQuestion(questions[questionNumber], timeLeft, score)
        # update score:
        # if got the correct answer:
        if askQuestion == True:
            # increment score
            score += 1
        # increment questionNumber
        questionNumber += 1
        # calculate remaining time
        remainingTime -= time.time() - startTime + 14
        # update time left
        timeLeft = remainingTime - 50
def askQuestion(questions, timeLeft, score):
    # initialize start time
    start = time.time()
    # correct answer number
    ansNumber = questions[6]
    # draw the question board:
    # clear screen
    Draw.clear()
    # set font family
    Draw.setFontFamily('Helvetica')
    # set font size
    Draw.setFontSize(16)
    # draw question box
    Draw.setColor(Draw.BLACK)
    Draw.rect(50, 50, 400, 100)
    # draw the question
    Draw.string(questions[1], 50, 60)
    # set font size for answer
    Draw.setFontSize(20)
    # draw answerBox 0
    answerBox0 = Draw.rect(50, 150, 400, 75)
    # draw answer 0
    Draw.string(questions[2], 50, 160)
    # draw answerBox 1
    answerBox1 = Draw.rect(50, 225, 400, 75)
    # draw answer 1
    Draw.string(questions[3], 50, 235)
    # draw answerBox 2
    answerBox2 = Draw.rect(50, 300, 400, 75)
    # draw answer 2
    Draw.string(questions[4], 50, 310)
    # draw answerBox 3
    answerBox3 = Draw.rect(50, 375, 400, 75)
    # draw answer 3
    Draw.string(questions[5], 50, 385)  
    # draw remainingTime
    Draw.string('Remaining Time: ' + str(int(timeLeft)), 55, 465)             
        
    # while time is remaining:
    while timeLeft > 0:        
        # if the user clicked
        if Draw.mousePressed() == True:
            Draw.clear()
            # get x, y of the click
            x = Draw.mouseX()
            y = Draw.mouseY()
            # if clicked in answerBox0:
            if x > 50 and x < 50 + 400 and y > 150 and y < 150 + 75:
                # if the correct answer number is 0:
                if ansNumber == 0:
                    # set font size
                    Draw.setFontSize(32)
                    # display good message
                    Draw.string('GOOD JOB!', 200, 200)                    
                    return True
                else:
                    # set font size
                    Draw.setFontSize(32)
                    # display negative message
                    Draw.string('Incorrect', 200, 200)
                    return False
            # if clicked in answerBox1:
            elif x > 50 and x < 50 + 400 and y > 225 and y < 225 + 75:
                # if the correct answer number is 1:
                if ansNumber == 1:
                    # set font size
                    Draw.setFontSize(32)
                    # display good message
                    Draw.string('GOOD JOB!', 200, 200)                    
                    return True
                else:
                    # set font size
                    Draw.setFontSize(32)
                    # display negative message
                    Draw.string('Incorrect', 200, 200)                    
                    return False
            # if clicked in answerBox2:
            elif x > 50 and x < 50 + 400 and y > 300 and y < 300 + 75:
                # if the correct answer number is 2:
                if ansNumber == 2:
                    # set font size
                    Draw.setFontSize(32)
                    # display good message
                    Draw.string('GOOD JOB!', 200, 200)                    
                    return True
                else:
                    # set font size
                    Draw.setFontSize(32)
                    # display negative message
                    Draw.string('Incorrect', 200, 200)                    
                    return False
            # if clicked in answerBox3:
            elif x > 50 and x < 50 + 400 and y > 375 and y < 375 + 75:
                # if the correct answer number is 3:
                if ansNumber == 3:
                    # set font size
                    Draw.setFontSize(32)
                    # display good message
                    Draw.string('GOOD JOB!', 200, 200)                    
                    return True
                else:
                    # set font size
                    Draw.setFontSize(32)
                    # display negative message
                    Draw.string('Incorrect', 200, 200)                    
                    return False
            # initialize end time
            end = time.time()
            # update remaining time
            timeLapsed = end - start
            timeLeft -= timeLapsed
nachash
  • 13
  • 1
  • Welcome to SO! Is there any code in there that is not necessary to reproduce the problem? If so, perhaps such code could be removed. See the instructions for how to create a [mre] for more guidance. – starball Dec 29 '22 at 23:55
  • Welcome to Stack Overflow. In your own words, where the code says `if askQuestion == True:`, exactly why do you believe this condition would ever be met? (Hint: do you understand the difference between a five dollar bill, as a physical thing, and the process of paying someone five dollars, as an abstract concept?) – Karl Knechtel Dec 30 '22 at 00:05

2 Answers2

0
if askQuestion == True:

is not the way you test what the function returned. askQuestion is a reference to the function, not what it returned. You need to assign the result of the function to a variable:

        correct = askQuestion(questions[questionNumber], timeLeft, score)
        # update score:
        # if got the correct answer:
        if correct:
            # increment score
            score += 1

or just put the call directly in the if statement:

        # update score:
        # if got the correct answer:
        if askQuestion(questions[questionNumber], timeLeft, score):
            # increment score
            score += 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

The problem you have is that you are comparing askQuestion to True. askQuestion has been defined as a function and a function object is not equal to True. You probably meant to compare against the return value of askQuestion which you can obtain by calling it askQuestion(...).

# This calls your code but you never store the result
askQuestion(questions[questionNumber], timeLeft, score)
# This compares a function object against a boolean value, this will never be true
if askQuestion == True:

You probably wanted to do this instead:

if askQuestion(questions[questionNumber], timeLeft, score):

Note that the comparison to True is implicit here. I will further note that not all execution paths in askQuestion return a value so you could have a case where your function executes and returns Null (default value).

ApplePie
  • 8,814
  • 5
  • 39
  • 60