You are on page 1of 8

COMPUTER PROGRAMMING

Year 9 – Unit 9.04


Last week, you entered in this program called MathsQuiz.py

print("What is 2 + 2?")
answer = input ()
answer = int(answer)
if answer == 4:
print("Well done")
else:
print("Sorry the answer was 4")

It would be good to have 5 questions, rather than just 1. Copy the


code in your program and paste it below for each new question…
see next slide for example.
Question 1

Question 2

Question 3
With just a few lines of extra code, it should be possible to
maintain a score throughout a game and then give the user some
feedback at the end.

score = 0 #this defines variable score, and sets it as zero


print("What is 2 + 2?")
answer = input ()
(score is a new
answer = int(answer)
if answer == 4: variable)
print("Well done")
score = score + 1 #this increases score by one
else:
print("Sorry the answer was 4")

Add the two highlighted lines of code to your FIRST question only
For the remaining 4 questions, just add the line
score = score + 1 #this increases score by one
…as shown below

print("What is 13 + 80?")
answer = input ()
answer = int(answer)
if answer == 93:
print("Well done")
score = score + 1 #this increases score by one
else:
print("Sorry the answer was 93")
We would also like to show the score after the user has attempted
answering the question. Remember, the score is stored in a
variable called score and is simply print(score)

score = 0 #this defines variable score, and sets it as zero


print("What is 2 + 2?")
answer = input ()
answer = int(answer)
if answer == 4:
print("Well done")
score = score + 1 #this increases score by one
else:
print("Sorry the answer was 4")
print(score)
A more sophisticated way of presenting the score could be…

score = 0 #this defines variable score, and sets it as zero


print("What is 2 + 2?")
answer = input ()
answer = int(answer)
if answer == 4:
print("Well done")
score = score + 1 #this increases score by one
else:
print("Sorry the answer was 4")
print(“Your current score is: “ , score)
If you can, do all these programs in SCRIPT mode
(i.e. go to FILE, NEW WINDOW…then press F5 to run)
Task:Amend your Maths Quiz program so that it prompts the end
user for their name at the beginning of the program.

When the score is displayed, it should show the message…

[name_variable], your score is [score_variable]

This should be displayed after each attempt at a question.

Example
Tim, your score is 1

Save your Maths Quiz


game.

You might also like