Advertisement
francydafne

Math_Quiz

Dec 15th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. try:
  2.     print ("The 10 highest scores previously were")
  3.     with open ("highscore.txt", "r") as f:
  4.         highscore = f.read()
  5.         print (highscore)
  6.  
  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.     name = input("Name: ")
  22.     score = int(input("Score: "))        
  23.    
  24.     print ("Welcome to the Math Quiz")
  25.     print ("Can you answer four questions and score maximum points?")
  26.     print ("Question 1: What is the product of 2x2x2? ")
  27.  
  28.     answer = int(input("The result is: "))
  29.     if answer == 8:
  30.         print ("correct!")
  31.         score = score + 1
  32.         print ("Your score is " + str(score))
  33.     else:
  34.         print ("Sorry, the right answer is 8")
  35.         print ("Your score is " + str(score))
  36.        
  37.     print ("Question 2: What is the result of 4-2? ")
  38.     answer = int(input("The result is "))
  39.     if answer == 2:
  40.         print ("Perfect!")
  41.         score = score + 1
  42.         print ("Your score is " + str(score))
  43.     else:
  44.         print ("Sorry, your answer is uncorrect, it's 2")
  45.         print ("Your score is: " + str(score))
  46.  
  47.     print ("Question 3: What is the result of 8/2? ")
  48.     answer = int(input("The answer is "))
  49.     if answer == 4:
  50.         print ("You did well!")
  51.         score = score + 1
  52.         print ("Your score is " + str(score))
  53.     else:
  54.         print ("Sorry for your fault, the result is 4")
  55.         print ("Your score is " + str(score))
  56.        
  57.     print ("Question 4: What is the result of 4 * 4? ")
  58.     answer = int(input("The result is "))
  59.     if answer == 16:
  60.         print ("Well done!")
  61.         score = score + 1
  62.         print ("Your score is " + str(score))
  63.     else:
  64.         print ("Sorry, but the resul is different: 16")
  65.         print ("Your score is " + str(score))
  66.  
  67.     position = 0
  68.     for compare_score in scores:
  69.         if score < compare_score:
  70.             position = position + 1
  71.     scores.insert(position, score)
  72.     names.insert(position, name)
  73.  
  74.     scores = scores[:10]
  75.     names = names[:10]
  76.  
  77.     print ("HIGHSCORES")
  78.     with open ("highscore.txt", "w") as file:
  79.         for i in range(len(scores)):
  80.             file.write(names[i] + " " + str(scores[i]) + "\n")
  81.             print(names[i] + " " + str(scores[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement