Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Hi-Score from Python 103
- # Tony Goodhew
- try:
- print("The 10 highest scores previously were")
- with open("highscoreY.txt", "r") as f:
- highscore = f.read()
- print(highscore)
- except:
- print("Creating a new highscoreY.txt file")
- f = open('highscoreY.txt', 'w')
- f.close()
- scores = []
- names = []
- with open("highscoreY.txt", 'r') as file:
- for line in file:
- line = line.strip("\n")
- line = line.split(" ")
- names.append(line[0])
- scores.append(int(line[1]))
- # simulate game
- name = input("Name: ")
- score = int(input("Score: "))
- position = 0
- for compare_score in scores:
- if score < compare_score:
- position = position + 1
- scores.insert(position, score)
- names.insert(position, name)
- scores = scores[:10]
- names = names[:10]
- print("HIGHSCORES")
- with open("highscoreY.txt", 'w') as file:
- for i in range(len(scores)):
- file.write(names[i] + " " + str(scores[i]) + "\n")
- print(names[i] + " " + str(scores[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement