Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1.  
  2. try:
  3.     print("The 10 highest scores previously were")
  4.     with open("highscore.txt", "r") as f:
  5.         highscore = f.read()
  6.         print(highscore)
  7. except:
  8.     print("Creating a new highscore.txt file")
  9.     f = open('highscore.txt', 'w')
  10.     f.close()
  11.  
  12. while True:
  13.     scores = []
  14.     names = []
  15.     with open("highscore.txt", 'r') as file:
  16.         for line in file:
  17.             line = line.strip("\n")
  18.             line = line.split(" ")
  19.             names.append(line[0])
  20.             scores.append(int(line[1]))
  21.     score = 0
  22.     print("Welcome to the Maths Quiz")
  23.     name = input("Hello player, what is your name? ")
  24.     print("Can you answer three questions and score maximum points?")
  25.     print("Question 1: What is the product of 2x2x2?")
  26.     answer = input("Your answer :>> ")
  27.     if answer == "8":
  28.         print("Correct")
  29.         score += 1
  30.         print("Your score is", score)
  31.     else:
  32.         print("Incorrect, the answer is 8")
  33.         print("Your score is", score)
  34.     print("Question 2: On a clock face what is the size of the angle that the second hand turns in one second?")
  35.     answer = input("Your answer :>> ")
  36.     if answer == "6":
  37.         print("Correct")
  38.         score += 1
  39.         print("Your score is", score)
  40.     else:
  41.         print("Incorrect, the answer is 6 degrees")
  42.         print("Your score is", score)
  43.     print("Question 3: What is the quotient and remainder for 8 / 3?")
  44.     print("Please format your answer as quotient and remainder, for example the answer to 23/5 is 4r3")
  45.     answer = input("Your answer :>> ")
  46.     if answer == "2r2":
  47.         print("Correct")
  48.         score += 1
  49.         print("Your score is", score)
  50.     else:
  51.         print("Incorrect, the answer is 2r2")
  52.         print("Your score is", score)
  53.     print("Game over, your score is",score)
  54.  
  55.     position = 0
  56.     for highscore in scores:
  57.         if score < highscore:
  58.             position = position + 1
  59.     scores.insert(position, score)
  60.     names.insert(position, name)
  61.    
  62.     scores = scores[:10]
  63.     names = names[:10]
  64.    
  65.     print("HIGHSCORES")
  66.     with open("highscore.txt", 'w') as file:
  67.         for i in range(len(scores)):
  68.             file.write(names[i] + " " + str(scores[i]) + "\n")
  69.             print(names[i] + " " + str(scores[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement