maroph

Simple Maths Game with highscore list

Nov 4th, 2019
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. #
  3. # file: maths-game2.py
  4. #
  5. # run on a Raspberry PI:
  6. # chmod u+x maths-game2.py
  7. # ./maths-game2.py
  8. import sys
  9.  
  10. try:
  11.     # print the current highscore data
  12.     with open("highscore2.txt", "r") as f:
  13.         print("The 10 highest scores previously were")
  14.         highscore = f.read()
  15.         print(highscore)
  16. except:
  17.     # ups - highscore file doesn't exist, so we create a new empty file
  18.     print("Creating a new highscore file (highscore2.txt)")
  19.     with open('highscore2.txt', 'w') as f:
  20.         pass
  21.  
  22. name = ''
  23. while name == '':
  24.     # we need a name for the highscore
  25.     name = input("Your name (or q to quit the game):>> ")
  26.     if name == 'q':
  27.         sys.exit(0)
  28.  
  29. resp = "y"
  30. while resp == str("y") or resp == str(""):
  31.     # we use 2 arrays: 1 for the score and one for the name
  32.     # this to arrays are related by the index
  33.     # score[0] and names[0] address the first (highest score) entry
  34.     # in the highscore
  35.     scores = []
  36.     names = []
  37.     with open("highscore2.txt", 'r') as file:
  38.         for line in file:
  39.             line = line.strip("\n")
  40.             line = line.split(" ")
  41.             names.append(line[0])
  42.             scores.append(int(line[1]))
  43.  
  44.     score = 0
  45.     print("Welcome to the Maths Quiz")
  46.     print("Can you answer three questions and score maximum points?")
  47.  
  48.     print("Question 1: What is the product of 2x2x2?")
  49.     answer = int(input("Your answer :>> "))
  50.     if answer == 8:
  51.         print("Correct")
  52.         score = score + 1
  53.         print("Your score is ", score)
  54.     else:
  55.         print("Incorrect, the answer is 8")
  56.         print("Your score is ", score)
  57.  
  58.     print("Question 2: What is the product of 6x7?")
  59.     answer = int(input("Your answer :>> "))
  60.     if answer == 42:
  61.         print("Correct")
  62.         score = score + 1
  63.         print("Your score is ", score)
  64.     else:
  65.         print("Incorrect, the answer is 42")
  66.         print("Your score is ", score)
  67.  
  68.     print("Question 3: What is the product of 17+4?")
  69.     answer = int(input("Your answer :>> "))
  70.     if answer == 21:
  71.         print("Correct")
  72.         score = score + 1
  73.         print("Your score is ", score)
  74.     else:
  75.         print("Incorrect, the answer is 21")
  76.         print("Your score is ", score)
  77.  
  78.     # this round of the game is finished
  79.     # now we compare the current score with the
  80.     # available highscore data
  81.     position = 0
  82.     for compare_score in scores:
  83.         if score <= compare_score:
  84.             position = position + 1
  85.     scores.insert(position, score)
  86.     names.insert(position, name)
  87.     scores = scores[:10]
  88.     names = names[:10]
  89.  
  90.     print("HIGHSCORES")
  91.     with open("highscore2.txt", 'w') as file:
  92.         for i in range(len(scores)):
  93.             file.write(names[i] + " " + str(scores[i]) + "\n")
  94.             print(names[i] + " " + str(scores[i]))
  95.     print("\n")
  96.  
  97.     resp = input("New try ? [y/n]:>> ")
Advertisement
Add Comment
Please, Sign In to add comment