0

I am developing a game and i need to add a leader board to it. I have coded one which allows the user to add high scores, view high scores and erase all high score. Now I need to sort all the scores in order of highest to lowest. Here's my code currently [NOTE: I'm aware that this isn't the best menu but I will change that later on]:

choice = input(str("Would you like to add a high score to the leader board?: "))
if choice == "y":
    user1 = input(str("Enter username 1: "))
    hs1 = input(str("Enter high score: "))
    user2 = input(str("Enter username 2: "))
    hs2 = input(str("Enter high score: "))
    data1 = user1 + ": " + hs1
    data2 = user2 + ": " + hs2
    with open("leaderboard.txt","a") as file:
        file.write(data1)
        file.write("\n")
        file.write(data2)
        file.write("\n")
        print("Data added.")
elif choice == "n":
    final_list = []
    with open("leaderboard.txt","r") as file:
        first_list = file.readlines() 
        for i in first_list: 
            final_list.append(i.strip())
        print("Leader board")
        print("-------------")
        for count in range(0,len(final_list)):
            print(final_list[count])

else:
    with open("leaderboard.txt","w") as file:
        file.write(" ")
        print("leader board cleared.")

I would like the leader board to be displayed once ordered something like this:

1. James F: 32
2. Harris W: 18
3. Courtney J: 12

Thank you for reading!

  • I'd consider reading in the file to a python data structure, sorting that and then re-writing the file. I may be over complicating it though. Also, consider better protecting the erase feature as now any typo (ie capital Y instead of y) would lead to a cleared leaderboard. – user1558604 Nov 30 '19 at 19:29
  • i can code a stronger menu how i coded this one is just for my own referance. Also are you saying like using 2d arrays? – Evan Dilkes Nov 30 '19 at 19:31
  • Do these answer your question? [How to sort a list of strings?](https://stackoverflow.com/questions/36139/how-to-sort-a-list-of-strings) [How to sort a list of strings numerically?](https://stackoverflow.com/questions/3426108/how-to-sort-a-list-of-strings-numerically) – lh1395 Nov 30 '19 at 19:32
  • @EvanDilkes, I was thinking read the file in line by line. Store each line to a list and split the value on `": "`. This should give you a list of tuples. Then see this for sorting a list of tuples:https://www.geeksforgeeks.org/python-program-to-sort-a-list-of-tuples-by-second-item/ and then write the file over with the new sorted list. – user1558604 Nov 30 '19 at 19:36
  • i found another solution but i will look into what you have said, thank you for contributing! – Evan Dilkes Nov 30 '19 at 19:40

1 Answers1

0

I found that i can restructure how the data is saved to the text file using numerical data first like this:

user1 = input(str("Enter username 1: "))
    hs1 = input(str("Enter high score: "))
    user2 = input(str("Enter username 2: "))
    hs2 = input(str("Enter high score: "))
    data1 = hs1 + " - " + user1
    data2 = hs2 + " - " + user2

Now the data starts with a number, i can simply use .sort on my list to sort them, however they will be sorted lowest to biggest so i had to use .reverse() to flip the list around.