Python: rest of code not playing out when function returns True

Question:

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
Asked By: nachash

||

Answers:

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
Answered By: Barmar

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).

Answered By: ApplePie
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.