Advertisement
TonyGo

Untitled

Nov 4th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. # Hi-Score from Python 103
  2. # Tony Goodhew
  3.  
  4. try:
  5. print("The 10 highest scores previously were")
  6. with open("highscoreY.txt", "r") as f:
  7. highscore = f.read()
  8. print(highscore)
  9. except:
  10. print("Creating a new highscoreY.txt file")
  11. f = open('highscoreY.txt', 'w')
  12. f.close()
  13.  
  14. scores = []
  15. names = []
  16. with open("highscoreY.txt", 'r') as file:
  17. for line in file:
  18. line = line.strip("\n")
  19. line = line.split(" ")
  20. names.append(line[0])
  21. scores.append(int(line[1]))
  22. # simulate game
  23. name = input("Name: ")
  24. score = int(input("Score: "))
  25.  
  26. position = 0
  27. for compare_score in scores:
  28. if score < compare_score:
  29. position = position + 1
  30. scores.insert(position, score)
  31. names.insert(position, name)
  32. scores = scores[:10]
  33. names = names[:10]
  34.  
  35. print("HIGHSCORES")
  36. with open("highscoreY.txt", 'w') as file:
  37. for i in range(len(scores)):
  38. file.write(names[i] + " " + str(scores[i]) + "\n")
  39. print(names[i] + " " + str(scores[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement