Advertisement
brendan-stanford

highscore_top-10

Jan 19th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #import libraries
  2. from random import randint
  3. from time import sleep
  4.  
  5. #play/quit game function and highscore comparison
  6. def math_game(player_score, player_name):
  7. #load highscore
  8. try:
  9. print("The 10 highest scores previously were")
  10. with open("highscore.txt", "r") as f:
  11. highscore = f.read()
  12. print(highscore)
  13. except:
  14. print("Creating a new highscore.txt file")
  15. f = open('highscore.txt', 'w')
  16. f.close()
  17.  
  18. #leave condition to break infinite loop
  19. play_on = True
  20.  
  21. #load previous highscores and names into lists for comparison
  22. while play_on == True:
  23. scores = []
  24. names = []
  25. with open("highscore.txt", 'r') as file:
  26. for line in file:
  27. line = line.strip("\n")
  28. line = line.split(" ")
  29. names.append(line[0])
  30. scores.append(int(line[1]))
  31.  
  32. #compare highscore
  33. position = 0
  34. for compare_score in scores:
  35. #if less than compare score, move to next
  36. if player_score < compare_score:
  37. position = position + 1
  38. #insert new name and score then cut list to top 10 names
  39. scores.insert(position, player_score)
  40. names.insert(position, player_name)
  41. scores = scores[:10]
  42. names = names[:10]
  43.  
  44. #play/quit options
  45. print("HIGHSCORES")
  46. with open("highscore.txt", 'w') as file:
  47. for i in range(len(scores)):
  48. file.write(names[i] + " " + str(scores[i]) + "\n")
  49. print(names[i] + " " + str(scores[i]))
  50.  
  51. #update leave condition
  52. play_on = play_quit()
  53.  
  54. #main game function
  55. def new_game():
  56. print("Starting a new math game!")
  57. user_name = input("What is your name? ")
  58. score = 0
  59. turns = 0
  60. #run for 10 rounds
  61. while turns < 10:
  62.  
  63. #create two random variables between 0 and 9
  64. factor_A = randint(0,9)
  65. factor_B = randint(0,9)
  66.  
  67. #ask user to multiply variables
  68. try:
  69. question = int(input(str(factor_A) + " times " + str(factor_B) + " is:"))
  70.  
  71. #if the answer is correct, change score by 1, or else do not
  72. if question == factor_A * factor_B:
  73. print("Correct!")
  74. score += 1
  75. else:
  76. print("Incorrect")
  77. #report score
  78. print("Your score is: " + str(score))
  79. turns += 1
  80. except ValueError:
  81. print("Integers only!")
  82.  
  83. #recall play/quit function
  84. math_game(score, user_name)
  85.  
  86. #opening play/quit function with score of 0
  87. def play_quit():
  88. try:
  89. play = int(input("Choose [1] to play or any other number to quit:"))
  90. if play == 1:
  91. new_game()
  92. else:
  93. #return False to update leave condition
  94. print("Thanks for playing!")
  95. return False
  96. except ValueError:
  97. print("Integers only!")
  98. math_game(0)
  99.  
  100. #call play/quit function
  101. play_quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement