Advertisement
johnpentyrch

1p10

May 13th, 2020
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. try:
  2.     print("The 10 highest scores previously were")
  3.     with open("highscore2.txt", "r") as f:
  4.         highscore = f.read()
  5.         print(highscore)
  6. except:
  7.     print("Creating a new highscore.txt file")
  8.     f = open('highscore2.txt', 'w')
  9.     f.close()
  10.    
  11. while True:
  12.     scores = []
  13.     names = []
  14.     with open("highscore2.txt", 'r') as file:
  15.         for line in file:
  16.             line = line.strip("\n")
  17.             line = line.split(" ")
  18.             names.append(line[0])
  19.             scores.append(int(line[1]))
  20.  
  21.  
  22.     score = 0
  23.     print("Welcome to the Maths Quiz")
  24.     xname=input("Write your name, please: (Type done when finished) ")
  25.     if xname=='done': exit()
  26.     #change spaces to underscore in name as we use space as delimiter!
  27.     name = xname.replace(" ", "_")
  28.     print("Can you answer three questions and score maximum points?")
  29.     print("Question 1: What is the product of 2x2x2?")
  30.     answer = int(input("Your answer :>> "))
  31.     if answer == 8:
  32.         print("Correct")
  33.         score = score + 1
  34.         print("Your score is ", score)
  35.     else:
  36.         print("Incorrect, the answer is 8")
  37.         print("Your score is ", score)
  38.     print("Question 2: What is the product of 3x3x3?")
  39.     answer = int(input("Your answer :>> "))
  40.     if answer == 27:
  41.         print("Correct")
  42.         score = score + 1
  43.         print("Your score is ", score)
  44.     else:
  45.         print("Incorrect, the answer is 27")
  46.         print("Your score is ", score)
  47.     print("Question 3: What is the product of 4x4x4?")
  48.     answer = int(input("Your answer :>> "))
  49.     if answer == 64:
  50.         print("Correct")
  51.         score = score + 1
  52.         print("Your score is ", score)
  53.     else:
  54.         print("Incorrect, the answer is 64")
  55.         print("Your score is ", score)
  56.     position = 0
  57.     for compare_score in scores:
  58.         if score < compare_score:
  59.             position = position + 1
  60.     scores.insert(position, score)
  61.     names.insert(position, name)
  62.  
  63.     scores = scores[:10]
  64.     names = names[:10]
  65.     print("HIGHSCORES")
  66.     with open("highscore2.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