# 1_12_maths_game_highscore_table.py

Apr 16th, 2021
621
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # 1_12_maths_game_highscore_table.py
2. # Deal with input
3.
4. """
5. .    First, try the code with no highscores.txt file.
6. .     Does the game create the file?
7.        A. prints error message: No highscores file found
8.            and creates highscores.txt
9. .    Does the game save the name and scores correctly each time the game loops? Remember to test different scores!
10.        A.And writes names scores.
11. .    Does the high score table work as expected?
12.        A. Yes. Though I don't understand the positioning.
13. """
14.
15. print("Loading high scores")
16. scores = []
17. names = []
18.
19. try:
20.     with open("highscores.txt", "r") as f:
21.         for line in f:
22.             line = line.strip("\n")
23.             line = line.split(" ")
24.             names.append(line[0])
25.             scores.append(int(line[1]))
26. except FileNotFoundError:
27.     print("No highscores file found")
28.
29. while True:
30.
31.     # display the highscores
32.     print("Highscores")
33.     for pos in range(len(names)):
34.         print(pos + 1, names[pos], scores[pos])
35.
36.     score = 0
37.     print("Welcome to the Maths Quiz")
38.     print("Can you answer three questions and score maximum points?")
39.     name = input("Whats your name? ")
40.
41.     print("Question 1: What is the product of 2x2x2?")
42.     answer = int(input("Your answer :>> "))
43.     if answer == 8:
44.         print("Correct, That is 1 mark for you.")
45.         score = score + 1
46.     else:
47.         print("Incorrect, the answer is 8")
48.
49.     print("Question 2:  What is the product of 3x3 ? ")
50.     answer = int(input("Your answer :>> "))
51.     if answer == 9:
52.         print("Correct, Another 1 point scored.")
53.         score = score + 1
54.     else:
55.         print("Incorrect, the answer is 9")
56.
57.     # print("Question 3: What is the quotient and remainder for 8 / 3?")
58.     # print("Please format your answer as quotient and remainder, for example the answer to 23/5 is 4r3")
59.     print("Question 3: What is the product of 5x2 ?")
60.     """ For Text Answers >
61.    answer = input("Your answer :>> ")
62.    if answer == "2r2": """
63.     answer = int(input("Your answer :>> "))
64.     if answer == 10:
65.         print("Correct, you have scored another 1 point.")
66.         score = score + 1
67.     else:
68.         print("Incorrect, the answer is 10")
69.
70.     print("Your score is", score)
71.
72.     # add the new score to the highscores
73.     position = 0
74.     for compare_score in scores:
75.         if score < compare_score:
76.             position = position + 1
77.     scores.insert(position, score)
78.     names.insert(position, name)
79.
80.     scores = scores[:10]
81.     names = names[:10]
82.
83.     # output the new highscore
84.     print("Saving highscores")
85.     with open("highscores.txt", 'w') as f:
86.         for pos in range(len(names)):
87.             f.write(names[pos] + " " + str(scores[pos]) + "\n")
88.
RAW Paste Data

# Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×