Beginning coder needs help

hey guys, I’ve just started to learn python, I’ve watched a lot of tutorials and wanted to make something to help me sort items in a dungeons and dragons game. I keep looking through my code but I don’t understand what I’m doing wrong. Can anybody offer me some guidance or suggestions on how I might fix it. thank you in advance.

code is attached below

inventory = ['Axe', 'Great Sword', 'Stick', 'Heath Potion']
    
rarity = {
        " The Necronomicon":  500,
        "Wand": 100,
        "Great sword": 98,
        "Staff": 96,
        "Golden Bow": 94,
       "Alchemist's supplies": 87,
       "Shovel": 84,
        "Iron Sword": 80,
        "Bow": 78,
        "Health Potion": 75,
        "Potion of Speed": 75,
        "Dagger": 45,
        "Axe": 34,
       "Lamp": 15,
        "BackPack": 10,
        "Torch": 7,
        "Mess Kit": 5,
        "rock": 1,
        "Stick": 1,
        }
    
sort_inv = sorted(inventory, key=rarity.get, reverse=True)
print(sort_inv)

hi there, the problem is that your rarity.get doesn’t always work when the value is not found in the rarity object.

If you pass values that exist, the code will work.

For eg. “Great Sword” is not found because you have defined instead “Great sword”

1 Like

With your current code, you have to make sure that every item in inventory has a corresponding key in rarity. I see a typo in your inventory list which causes the key lookup to return None. Also, I see a typo in a key of rarity dictionary, that causes one of the list items to return None. You can not compare None to other items in the list, so the error is displayed.

1 Like

thanks guys, i fixed the spelling and it worked!

2 Likes