Step 7 – Adding a score
If this is a quiz then we need to include a scoring system.
This involves using a variable to keep score with.
THOERY: a variable is just an area of memory set aside to store a value. We already used a a variable – it was called answer and it stored the answer the player gave to the first question.
- Change your code in the three places shown in the screenshot
- First, set the score to 0
If we don't do this, Python won't know that the variable score exists
- Then add one to it when the user gets the question right
Score is a number (an integer data type), so it's easy to add or take away from it. If you wanted to be evil you could take a point away if the player gets the wrong answer...
- Then right at the bottom add the code to display the score
- Run your code and check that it all works
- You need to check what happens if you get the answer wrong and what happens if you get it right.
Make sure you test your code really carefully.