Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- scores = []
- names = []
- try:
- print("The 10 highest scores previously were")
- with open("highscore.txt", "r") as f:
- for line in f:
- line = line.strip("\n")
- line = line.split(" ")
- names.append(line[0])
- scores.append(int(line[1]))
- except:
- print("Creating a new highscore.txt file")
- f = open('highscore.txt', 'w')
- f.close()
- while True:
- score=0
- print("Welcome to the Maths Quiz")
- name=str(input("Write your name, please: "))
- print("Dear "+name+" 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 2³?")
- 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 3: What is the product of square root of 64?")
- 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)
- position = 0
- for compare_score in scores:
- if score < compare_score:
- position = position + 1
- scores.insert(position, score)
- names.insert(position, name)
- 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