 # Maths Quiz

May 5th, 2021
48
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from random import randint
2.
3. def getPlayerName():
4.     #get the players name
5.     name = input('Enter your name: ').strip()
6.     if name == '':
7.         name = 'Unknown'
8.     return name
9.
12.     scores = []
13.     names = []
14.     try:
15.         with open('highscore.txt','r') as f:
16.             for line in f:
17.                 line = line.strip('\n')
18.                 line = line.split(' ')
19.                 names.append(line)
20.                 scores.append(line)
21.     except FileNotFoundError:
22.         print('Creating New Highscore File')
23.         f = open('highscore.txt','w')
24.         f.write('Unknown 0')
25.         f.close()
26.     return names,scores
27.
28.
29. def displayHighScores(names,scores):
30.     #print highscore table
31.     print('')
32.     print('NAME','\t','SCORE')
33.     for pos in range(len(names)):
34.         print(names[pos],'\t',scores[pos])
35.     print('')
36.
37.
38. def updateHighscores(names,name,scores,score):
39.     #find the correct position for current score and insert into names scores lists
40.     pos = 0
41.     for current in scores:
42.         if score < int(current):
43.             pos += 1
44.     scores.insert(pos,score)
45.     names.insert(pos,name)
46.     scores = scores[:10]
47.     names = names[:10]
48.     return names,scores
49.
50. def saveHighscores(names,scores):
51.     #write names and scores lists to highscore textfile
52.     try:
53.         with open('highscore.txt','w') as f:
54.             for pos in range(len(scores)):
55.                 f.write(names[pos] + ' ' + str(scores[pos]) + '\n')
56.     except FileNotFoundError:
57.         print('Creating New Highscore File')
58.         f = open('highscore.txt','w')
59.         f.write('Unknown 0')
60.         f.close()
61.
62.
65.     a = randint(1,5)
66.     b = randint(1,7)
67.     c = randint(1,9)
68.     answer = a * b * c
69.     notValid = True
70.     while notValid:
71.         try:
72.             response = int(input('{}. What is the product of {} * {} * {} ? '.format(question,a,b,c)))
73.             notValid = False
74.         except:
75.             print('Invalid Input')
76.             notValid = True
78.
79.
80. def playAgain():
81.     again = input('Do you want to play again (Y/N) ?').upper()
82.     if again == 'Y':
83.         return True
84.     else:
85.         return False
86.
87. def welcome():
88.     print('Welcome to the maths quiz')
89.     print('Answer as many questions as you can')
90.
91. def goodbye():
92.     print('Goodbye, thank you for playing')
93.
94. def quiz():
95.     #ask questions until player gets one wrong
96.     name = getPlayerName()
97.     score = 0
99.     question = 0
101.         question += 1
104.             score += 1
105.             #print('Correct, Score is {}'.format(score))
106.         else:
108.             print('{}, you scored {}'.format(name,score))
110.     return name,score
111.
112. def game(names,scores):
113.     again = True
114.     while again:
115.         name,score = quiz()
116.         names,scores = updateHighscores(names,name,scores,score)
117.         saveHighscores(names,scores)
118.         again = playAgain()
119.     return names,scores
120.
121. #function calls
122. welcome()