Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- print("The 10 highest scores previously were")
- with open("highscore.txt", "r") as f:
- highscore = f.read()
- print(highscore)
- except:
- print("Creating a new highscore.txt file")
- f = open('highscore.txt', 'w')
- f.close()
- while True:
- scores = []
- names = []
- with open("highscore.txt", 'r') as file:
- for line in file:
- line = line.strip("\n")
- line = line.split(" ")
- names.append(line[0])
- scores.append(int(line[1]))
- score = 0
- print("Welcome to the Maths Quiz")
- name = input("Hello player, what is your name? ")
- print("Can you answer three questions and score maximum points?")
- print("Question 1: What is the product of 2x2x2?")
- answer = input("Your answer :>> ")
- if answer == "8":
- print("Correct")
- score += 1
- print("Your score is", score)
- else:
- print("Incorrect, the answer is 8")
- print("Your score is", score)
- print("Question 2: On a clock face what is the size of the angle that the second hand turns in one second?")
- answer = input("Your answer :>> ")
- if answer == "6":
- print("Correct")
- score += 1
- print("Your score is", score)
- else:
- print("Incorrect, the answer is 6 degrees")
- print("Your score is", score)
- print("Question 3: What is the quotient and remainder for 8 / 3?")
- print("Please format your answer as quotient and remainder, for example the answer to 23/5 is 4r3")
- answer = input("Your answer :>> ")
- if answer == "2r2":
- print("Correct")
- score += 1
- print("Your score is", score)
- else:
- print("Incorrect, the answer is 2r2")
- print("Your score is", score)
- print("Game over, your score is",score)
- position = 0
- for highscore in scores:
- if score < highscore:
- position = position + 1
- scores.insert(position, score)
- names.insert(position, name)
- scores = scores[:10]
- names = names[:10]
- print("HIGHSCORES")
- with open("highscore.txt", 'w') as file:
- for i in range(len(scores)):
- file.write(names[i] + " " + str(scores[i]) + "\n")
- print(names[i] + " " + str(scores[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement