0

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!")
martineau
  • 119,623
  • 25
  • 170
  • 301
MulleMeck
  • 1
  • 1
  • Please edit your code, there are indentation errors. – Funpy97 Jun 06 '21 at 18:56
  • 1
    If you don't know how to ask your question you can read [ask] – peer Jun 06 '21 at 18:57
  • Fixed intendation errors. – MulleMeck Jun 06 '21 at 19:11
  • Does this answer your question? [Storing Python dictionaries](https://stackoverflow.com/questions/7100125/storing-python-dictionaries) – peer Jun 06 '21 at 19:57
  • Which kind of file do you want? Just a storage for your program? Then `pickle` your data. A human-readable, editable file? Use `csv` or `json` or even `configparser` to build a windows INI-like file; or with just a little more effort, the XML tools – gimix Jun 06 '21 at 20:05
  • @gimix The file should be able to save the dictionary, and then return the contents from the file in the python console. Is that possible with pickle? – MulleMeck Jun 06 '21 at 21:07
  • @MulleMeck: Yes, `pickle` can do that. – martineau Jun 06 '21 at 21:23

1 Answers1

0

So your code should be something like this:

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', 'wb') as f:
        pickle.dump(users, f)

    elif choice == "2":
        with open('highscore.txt', 'rb') as f:
            users = pickle.load(f)
    
    elif choice == "quit":
        print("\nThank you for using this program!")

    else:
        print("\nSomething went wrong, try again!")
gimix
  • 3,431
  • 2
  • 5
  • 21