Python 6.6 : Exercise : Conditions
Now that the math game prints a score, we want it to react to that score with a personalised message. The exercise is to compare the number of points with the number of questions: a perfect score prints "Excellent", zero points prints "You have to revise your math". For the cases in between, we compute the half-average (the number of questions divided by two) and decide between "Not bad" and "Can do better". Try it on your own, then look at the correction.
Chaining the conditions
In the correction we chain if, elif and else. The first branch checks the perfect score, the second the zero score. Then we compute the average. Because dividing 5 by 2 returns the float 2.5, we cast it to int so the comparison stays clean. The third branch handles the case where the score is above this threshold, and the final else covers the remaining situation.
if nb_points == NUMBER_OF_QUESTIONS:
print("Excellent")
elif nb_points == 0:
print("You have to revise your math")
else:
average = int(NUMBER_OF_QUESTIONS / 2)
if nb_points > average:
print("Not bad")
else:
print("Can do better")
We can validate every branch by playing a few rounds. A perfect 5/5 prints "Excellent". A 4/5 still prints "Not bad". A 2/5 prints "Can do better". And typing only wrong answers ends with "You have to revise your math". The final message gives a nice closing to the session. In the next video we will let the user choose between addition and multiplication so the game becomes a little richer.
Summary
This lesson demonstrates a Python exercise on conditional statements where students receive feedback based on comparing their quiz score to the total number of questions. The exercise uses if/elif statements to evaluate whether a score equals the maximum points (returns "excellent"), equals zero (returns "revise your mathematics"), or falls above/below the calculated average. The instructor converts the average from a float to an integer value, then tests the exercise with multiple student results to verify the conditional logic and output messages.
Key points
- Using if/elif conditional statements to compare numerical scores and provide dynamic feedback
- Converting float averages to integers using int() to ensure proper data type handling
- Implementing multiple conditional branches: excellent (full score), revise math (zero score), not bad (above average), can do better (below average)
- Comparing scores against multiple thresholds (equal to total questions, equal to zero, greater than/less than calculated average)
- Testing exercise logic with various input values to validate conditional branches and output messages
FAQ
Why do we need to convert the average to an integer?
Because the average calculated (such as 2.5) is a float value in Python. Converting it to an integer type using int() ensures consistent comparison operations and proper data type handling for the conditional logic.
What feedback does the program display when the score equals the total number of questions?
The program returns 'excellent' when the student's score equals the number of questions, indicating a perfect score.
What message is displayed if a student scores zero points?
If the score equals zero, the program displays 'you must revise your mathematics' to prompt the student to review their math skills.