Week 5 Computer Programming Year 9 – Unit 9.04 Gray 80 69 69 69, Calibri 24 Dark Red RGB 153 0 0, Calibri 54
print("Sorry the answer was 4") 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
(score is a new variable) 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 () answer = int(answer) if answer == 4: print("Well done") score = score + 1 #this increases score by one else: print("Sorry the answer was 4") (score is a new variable) Add the two highlighted lines of code to your FIRST question only
score = score + 1 #this increases score by one 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)
print(“Your current score is: “ , 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)
[name_variable], your score is [score_variable] 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.