Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- print("The 10 highest scores previously were")
- with open("highscore2.txt", "r") as f:
- highscore = f.read()
- print(highscore)
- except:
- print("Creating a new highscore.txt file")
- f = open('highscore2.txt', 'w')
- f.close()
- while True:
- scores = []
- names = []
- with open("highscore2.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")
- xname=input("Write your name, please: (Type done when finished) ")
- if xname=='done': exit()
- #change spaces to underscore in name as we use space as delimiter!
- name = xname.replace(" ", "_")
- print("Can you answer three questions and score maximum points?")
- print("Question 1: What is the product of 2x2x2?")
- answer = int(input("Your answer :>> "))
- if answer == 8:
- print("Correct")
- score = score + 1
- print("Your score is ", score)
- else:
- print("Incorrect, the answer is 8")
- print("Your score is ", score)
- print("Question 2: What is the product of 3x3x3?")
- answer = int(input("Your answer :>> "))
- if answer == 27:
- print("Correct")
- score = score + 1
- print("Your score is ", score)
- else:
- print("Incorrect, the answer is 27")
- print("Your score is ", score)
- print("Question 3: What is the product of 4x4x4?")
- answer = int(input("Your answer :>> "))
- if answer == 64:
- print("Correct")
- score = score + 1
- print("Your score is ", score)
- else:
- print("Incorrect, the answer is 64")
- print("Your score is ", score)
- position = 0
- for compare_score in scores:
- if score < compare_score:
- position = position + 1
- scores.insert(position, score)
- names.insert(position, name)
- scores = scores[:10]
- names = names[:10]
- print("HIGHSCORES")
- with open("highscore2.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