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]))
- name = input("Name: ")
- score = int(input("Score: "))
- print ("Welcome to the Math Quiz")
- print ("Can you answer four questions and score maximum points?")
- print ("Question 1: What is the product of 2x2x2? ")
- answer = int(input("The result is: "))
- if answer == 8:
- print ("correct!")
- score = score + 1
- print ("Your score is " + str(score))
- else:
- print ("Sorry, the right answer is 8")
- print ("Your score is " + str(score))
- print ("Question 2: What is the result of 4-2? ")
- answer = int(input("The result is "))
- if answer == 2:
- print ("Perfect!")
- score = score + 1
- print ("Your score is " + str(score))
- else:
- print ("Sorry, your answer is uncorrect, it's 2")
- print ("Your score is: " + str(score))
- print ("Question 3: What is the result of 8/2? ")
- answer = int(input("The answer is "))
- if answer == 4:
- print ("You did well!")
- score = score + 1
- print ("Your score is " + str(score))
- else:
- print ("Sorry for your fault, the result is 4")
- print ("Your score is " + str(score))
- print ("Question 4: What is the result of 4 * 4? ")
- answer = int(input("The result is "))
- if answer == 16:
- print ("Well done!")
- score = score + 1
- print ("Your score is " + str(score))
- else:
- print ("Sorry, but the resul is different: 16")
- print ("Your score is " + str(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 ("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