Python 6.5 : Exercise : Points

In this exercise we add scoring to our math game. The plan is simple: create a variable nb_points initialised to zero, increment it by one each time the user gives the correct answer, and finally print the result with a message like "Your score is 3 out of 5". The score has to be counted in the main for loop, which means the question function has to tell the caller whether the answer was correct or not.

Returning True or False from the function

In the correction we modify question so that it returns True when the user's answer matches a + b and False otherwise. The print of "correct" or "incorrect" still happens inside the function, but now the boolean return value lets the loop know what to do with the score.

def question():
    a = random.randint(MIN, MAX)
    b = random.randint(MIN, MAX)
    answer_int = int(input("Calculate %s + %s = " % (a, b)))
    if answer_int == a + b:
        print("Correct answer")
        return True
    else:
        print("Incorrect answer")
        return False

nb_points = 0
for i in range(NUMBER_OF_QUESTIONS):
    if question():
        nb_points += 1

print("Your score is: %s / %s" % (nb_points, NUMBER_OF_QUESTIONS))

We can test the behaviour by temporarily setting NUMBER_OF_QUESTIONS to 2: after answering both questions, the program prints something like "Your score is: 1 / 2". Putting it back to 5 and giving four correct answers prints "Your score is: 4 / 5", which is exactly what we wanted. In the next exercise we will use this score to display a final message depending on how well the user did.

Summary

This Python lesson teaches how to implement a scoring system for a quiz or question-answer exercise. You'll initialize a points counter at zero, increment it each time a correct answer is provided, and display the final score as both a count and a percentage. The implementation uses loops, functions, and conditional statements to verify answers and track progress.

Key points

  • Initialize a points counter variable to zero before the question loop to track correct answers
  • Increment the points counter (nb_points += 1) inside a conditional block when the answer is verified as correct
  • Use if statements to validate whether user input matches the expected result (e.g., a + b)
  • Return the points count or boolean from the function to indicate correct/incorrect answers
  • Calculate and display the final score as a percentage by dividing points by total questions and multiplying by 100
  • Use print statements to display formatted output showing the user's score (e.g., 'Your note: 4/6 = 67%')

FAQ

How should we initialize the points counter in Python?

Create a variable before the loop: `nb_points = 0`. This variable will accumulate each time a correct answer is detected.

When and where should we increment the points?

Increment inside the if statement that verifies correctness, using `nb_points += 1`. This ensures points are only added for correct answers.

How do we display the final score to the user?

Use a print statement with the formula: `print(f'Your note: {nb_points}/{total_questions} = {(nb_points/total_questions)*100:.0f}%')` to show both the count and percentage.