-3

Here I want my code to let the user enter a game and then enter a rating, and the loop puts it in a dictionary but what I would like to do it sort the dictionary (games) by the ratings that the user input for the games

games = []
def gamef():
    print("Here you will type your favorite games and then rate then out of 10 and this will sort them for you. ")
    while True:
        name = input("Enter your game for the games list: ")
        rating = [int(i) for i in input("Enter your rating for the game: ")]

        games.append({
            "Game Title": name,
            "Game Rating": rating
        })
        cont = input("Want to add another? (Y/N)")
        if cont == "N":
            break;
        if cont == "n":
            break;

gamef()

print("Here's your games list: ")
print(games)

games.sort() # <-- Need help here.

print("Here's your list of games in order by rating.")
print(games)

I would like this to sort the dictionary by the rating and then print it. Please help me make the code sort it. How should I sort a dictionary based off of its values, where many of the values will have repeated, non-unique entries?

tehhowch
  • 9,645
  • 4
  • 24
  • 42
  • What did you try already? Many of us **need** you to actually make an attempt in order to justify responding. – tehhowch Mar 06 '18 at 16:14
  • 3
    Check this https://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-values-of-the-dictionary-in-python – Nathan Mar 06 '18 at 16:16
  • 1
    Possible duplicate of [How do I sort a list of dictionaries by values of the dictionary in Python?](https://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-values-of-the-dictionary-in-python) – SiHa Mar 06 '18 at 19:45

2 Answers2

0

There's no need to have "Game Title" and "Game Rating" as the key for a series of dictionaries, and regardless, dictionaries are inherently orderless, so you would have to make a list out of the dictionary entries and sort that, but I don't think this would work too well since your game ratings will not be unique entries.

Why not use a pandas dataframe? You can make two columns of data, then sort based off of one of the columns

##To Setup the DataFrame
import pandas as pd    
Games= pd.DataFrame(columns=["Game Name","Game Rating"])
##To append a row
Appending_Row=pd.Dataframe([[name,rating],columns=["Game Name","Game Rating"])
Games.append(Appending_Row)

Then you can use sort_values, as answered here

how to sort pandas dataframe from one column

Novice
  • 855
  • 8
  • 17
0

I got help from one of my friends I know that this is kinda pointless but it's for school so. Here's my code:

games = {}
def gamef():

print("Here you will type your favorite games and then rate them from 0-9 9 being the highest and this will sort them for you. ")
while True:
    name = input("Enter your game for the games list: ")
    rating = [int(i) for i in input("Enter your rating for the game: ")]

    games.update({name:rating})

    cont = input("Want to add another game? (Y/N)")
    if cont == "N":
        break;
    if cont == "n":
        break;
gamef()

print("Here's your games list: ")
print(games)

print("Here is your games sorted in order of what you rated them: ")

for w in sorted(games, key=games.get, reverse = True):
    print(w, str(games[w]))