Advertisement
JAS_Software

Maths Quiz

May 5th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  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.  
  10. def loadHighscores():
  11.     #load highscores and add to scores and names lists
  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[0])
  20.                 scores.append(line[1])            
  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.    
  63. def askQuestion(question):
  64.     #ask math question
  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
  77.     return answer,response
  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
  98.     ask = True
  99.     question = 0
  100.     while ask:
  101.         question += 1
  102.         answer, response = askQuestion(question)
  103.         if answer == response:
  104.             score += 1
  105.             #print('Correct, Score is {}'.format(score))
  106.         else:
  107.             print('Incorrect. The correct answer is {}'.format(answer))
  108.             print('{}, you scored {}'.format(name,score))
  109.             ask = False
  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()
  123. names,scores = loadHighscores()
  124. displayHighScores(names,scores)
  125. names,scores = game(names,scores)
  126. displayHighScores(names,scores)
  127. goodbye()
  128.  
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement