Advertisement
xavicano

Untitled

May 11th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. scores = []
  2. names = []
  3.  
  4. try:
  5.     print("The 10 highest scores previously were")
  6.     with open("highscore.txt", "r") as f:
  7.         for line in f:
  8.             line = line.strip("\n")
  9.             line = line.split(" ")
  10.             names.append(line[0])
  11.             scores.append(int(line[1]))
  12. except:
  13.     print("Creating a new highscore.txt file")
  14.     f = open('highscore.txt', 'w')
  15.     f.close()
  16.  
  17.  
  18. while True:
  19.     score=0
  20.     print("Welcome to the Maths Quiz")
  21.     name=str(input("Write your name, please: "))
  22.     print("Dear "+name+" can you answer three questions and score maximum points?")
  23.     print("Question 1: What is the product of 2x2x2?")
  24.     answer = int(input("Your answer :>> "))
  25.     if answer == 8:
  26.         print("Correct")
  27.         score = score + 1
  28.         print("Your score is ", score)
  29.     else:
  30.         print("Incorrect, the answer is 8")
  31.         print("Your score is ", score)
  32.     print("Question 2: What is the product of 2³?")
  33.     answer = int(input("Your answer :>> "))
  34.     if answer == 8:
  35.         print("Correct")
  36.         score = score + 1
  37.         print("Your score is ", score)
  38.     else:
  39.         print("Incorrect, the answer is 8")
  40.         print("Your score is ", score)
  41.     print("Question 3: What is the product of square root of 64?")
  42.     answer = int(input("Your answer :>> "))
  43.     if answer == 8:
  44.         print("Correct")
  45.         score = score + 1
  46.         print("Your score is ", score)
  47.     else:
  48.         print("Incorrect, the answer is 8")
  49.         print("Your score is ", score)
  50.     position = 0
  51.     for compare_score in scores:
  52.         if score < compare_score:
  53.             position = position + 1
  54.     scores.insert(position, score)
  55.     names.insert(position, name)
  56.     print("HIGHSCORES")
  57.     with open("highscore.txt", 'w') as file:
  58.         for i in range(len(scores)):
  59.             file.write(names[i] + " " + str(scores[i]) + "\n")
  60.             print(names[i] + " " + str(scores[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement