- Demonstrate use of variables to add a score to the game
- Designing a coding solution using pseudocode
- Add responsive score feedback to the game
Words to learn: variable, integer, string, convert, pseudocode
Starter – Adding Scores
In the last lesson, you used the following script to create the beginning of a maths quiz and then developed it into a 12 times table test:
The quiz is OK, but it doesn’t give us much reason to keep coming back to it. If we added in a score system would help engage people as they would want to get the highest score possible. So how can we do this?
We need to add a variable called score to keep track of… well the score! Change the code from above to the following:
At the end of your script you will want to display the final score. Try the following:
print("The final score is: " + score)
Try running it – you will get the following error:
Why do you think you get this error?
The reason is the score is stored as an integer (whole number) but the print command uses strings (text) to display things on the screen. We therefore need to tell Python to convert the integer into a string. So let’s change our code to the following:
print("The final score is: " + str(score))
The str command tells Python to convert the integer to a string.
You can also add a conditional if to give you interactive feedback:
Main Task 1 – Understanding Pseudocode
In the next task we are going to alter your 12 times table program so that it keep score and gives feedback on how good (or bad) the student’s score was. To work out what we are going to do we can use a design technique called pseudocode.
Pseudocode is a bit like a cross between normal English and a programming language (like Python). It is designed so you can write out a possible solution to a problem, but you don’t get “bogged down” in how to write it as code. The following example shows pseudocode for a teacher entering 10 class grades to get an average:
Set total to zero Set grade counter to one
While grade counter is less than or equal to ten Input the next grade Add the grade into the total Set the class average to the total divided by ten
Print the class average
Main Task 2 – Adding Responsive Feedback
You will be altering your 12 times table program. Your program will still ask between 4 and 12 questions on the 12 times table, but it will now also keep track of the score and give appropriate feedback. For example if you ask 8 questions: if the user gets 2 or less then they will be told “that’s not very good!”; if the user gets between 3 and 5 marks they will be told “satisfactory”; if the user gets 6 or more marks they will be told “well done”.
Before we start coding the program, you need to design the pseudocode. Use this worksheet to help design your pseudocode. When you have finished, save the worksheet, print it out and put it into your folder.
Once you have created your pseudocode, use that to produce a solution using Python to the problem set above.
Once you have finished and checked your Python program works, print out the code and put it into your folder as well.