I'm writing a program for a school project in which I have to store high scores from user inputs. I have most of the program done, except for reading/writing from/to the file where I'm completely clueless.
The instructions said you should be able to have more than five users, but only the five best would be saved to the file. The scores should be sorted from highest to lowest, and a user is supposed to be able to update their previous score.
This is what I have so far:
class highscoreUser:
def __init__(self, name, score):
self.name = name
self.score = score
@classmethod
def from_input (cls):
return cls(
input("Type the name of the user:"),
float(input("Type the users score")),
)
print("Welcome to the program! Choose if you want to continue using this program or if you want to quit! \n -Press anything to continue. \n -or type quit to exit the program.")
choice = input("What would you like to do?:")
choice = choice.lower()
while choice != "quit":
try:
NUMBER = int(input ("How many people are adding their highscores?"))
except:
print("Something went wrong, try again!")
choice !="quit"
users = {}
try:
for i in range(NUMBER):
user = highscoreUser.from_input()
users[user.score] = user
except:
print("")
choice !="quit"
This is where I need help:
while choice != "quit":
print("\n.Now you have multiple choices: \n1.Uppdatera highscore. \n2.Hämta highscore \n3.Quit to exit the program ")
choice = input("What would you like to do?:")
choice = choice.lower()
# if choice == "1":
# with open("highscore.txt", "w") as scoreFile:
# ###Function to write/update the file with the user dictionary
# elif choice == "2":
# with open("highscore.txt", "r") as scoreFile
# ###Function for reading the highscore file
# elif choice == "quit":
# print("\nThank you for using this program!")
# else:
# print("\nSomething went wrong, try again!")