Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- def getPlayerName():
- #get the players name
- name = input('Enter your name: ').strip()
- if name == '':
- name = 'Unknown'
- return name
- def loadHighscores():
- #load highscores and add to scores and names lists
- scores = []
- names = []
- try:
- with open('highscore.txt','r') as f:
- for line in f:
- line = line.strip('\n')
- line = line.split(' ')
- names.append(line[0])
- scores.append(line[1])
- except FileNotFoundError:
- print('Creating New Highscore File')
- f = open('highscore.txt','w')
- f.write('Unknown 0')
- f.close()
- return names,scores
- def displayHighScores(names,scores):
- #print highscore table
- print('')
- print('NAME','\t','SCORE')
- for pos in range(len(names)):
- print(names[pos],'\t',scores[pos])
- print('')
- def updateHighscores(names,name,scores,score):
- #find the correct position for current score and insert into names scores lists
- pos = 0
- for current in scores:
- if score < int(current):
- pos += 1
- scores.insert(pos,score)
- names.insert(pos,name)
- scores = scores[:10]
- names = names[:10]
- return names,scores
- def saveHighscores(names,scores):
- #write names and scores lists to highscore textfile
- try:
- with open('highscore.txt','w') as f:
- for pos in range(len(scores)):
- f.write(names[pos] + ' ' + str(scores[pos]) + '\n')
- except FileNotFoundError:
- print('Creating New Highscore File')
- f = open('highscore.txt','w')
- f.write('Unknown 0')
- f.close()
- def askQuestion(question):
- #ask math question
- a = randint(1,5)
- b = randint(1,7)
- c = randint(1,9)
- answer = a * b * c
- notValid = True
- while notValid:
- try:
- response = int(input('{}. What is the product of {} * {} * {} ? '.format(question,a,b,c)))
- notValid = False
- except:
- print('Invalid Input')
- notValid = True
- return answer,response
- def playAgain():
- again = input('Do you want to play again (Y/N) ?').upper()
- if again == 'Y':
- return True
- else:
- return False
- def welcome():
- print('Welcome to the maths quiz')
- print('Answer as many questions as you can')
- def goodbye():
- print('Goodbye, thank you for playing')
- def quiz():
- #ask questions until player gets one wrong
- name = getPlayerName()
- score = 0
- ask = True
- question = 0
- while ask:
- question += 1
- answer, response = askQuestion(question)
- if answer == response:
- score += 1
- #print('Correct, Score is {}'.format(score))
- else:
- print('Incorrect. The correct answer is {}'.format(answer))
- print('{}, you scored {}'.format(name,score))
- ask = False
- return name,score
- def game(names,scores):
- again = True
- while again:
- name,score = quiz()
- names,scores = updateHighscores(names,name,scores,score)
- saveHighscores(names,scores)
- again = playAgain()
- return names,scores
- #function calls
- welcome()
- names,scores = loadHighscores()
- displayHighScores(names,scores)
- names,scores = game(names,scores)
- displayHighScores(names,scores)
- goodbye()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement