Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While creating a Dictionary: TypeError: unhashable type: 'dict'

I'm trying to script a simple Character Generator that I can use for Pen and Paper RPG's. I was thinking about storing all my information in a nested dictionary and saving it into a JSON file.

However, while creating the following dictionary, I receive as error:

nhashable type: 'dict', focussing on {'cha': 1}}}

core_phb = {
'races': {
    'Human': {
        {'abilities': 'None'},
        {'alignment': 'Neutral'},
        {'size': 'Medium'},
        {'speed': 6},
        {'languages': 'Common'},
        {'ability_modifiers': {
            {'str': 1},
            {'dex': 1},
            {'con': 1},
            {'int': 1},
            {'wis': 1},
            {'cha': 1}}}
    },
    'Dwarf': {
        {'abilities': [
            'ability1',
            'ability2'
            ]},
        {'alignment': 'Lawful Good'},
        {'size': 'Medium'},
        {'speed': 5},
        {'languages': [
            'Common',
            'Dwarven'
            ]},
        {'ability_modifiers': [
            {'con': 2},
            {'wis': 1}
            ]}
    },
    'Elf': {
        {'abilities': [
            'ability1',
            'ability2'
            ]},
        {'alignment': 'Chaotic Good'},
        {'size': 'Medium'},
        {'speed': 6},
        {'languages': [
            'Common',
            'Elven'
            ]},
        {'ability_modifiers': [
            {'dex': 2},
            {'int': 1}
            ]}
    }
},
'classes': {
    {'Fighter': {}},
    {'Ranger': {}},
    {'Wizard': {}}
},
'ability_scores': [
    {'Str': 'str'},
    {'Dex': 'dex'},
    {'Con': 'con'},
    {'Int': 'int'},
    {'Wis': 'wis'},
    {'Cha': 'cha'}]
}

I am simply trying to create the dictionary, not calling any keys from it.

As I understand from TypeError: unhashable type: 'dict' , I can use frozenset() to get keys.

Is there a better way to do what I am trying to do?

like image 329
Steven Pincé Avatar asked Jan 25 '26 10:01

Steven Pincé


1 Answers

You seem to be making dictionaries {...} incorrectly for Python.

Lists look like this:

[ {'a': 1}, {'b': 1}, {'c': 1} ]

Dictionaries look like this:

{ 'a': 1, 'b': 2, 'c': 3 }

If I'm guessing the behavior you want correctly, then you probably wanted something like this:

human = {
    'abilities': 'None',
    'alignment': 'Neutral',
    'size': 'Medium',
    'speed': 6,
    'languages': 'Common',
    'ability_modifiers': {
        'str': 1,
        'dex': 1,
        'con': 1,
        'int': 1,
        'wis': 1,
        'cha': 1
    }
}
like image 160
John Starich Avatar answered Jan 28 '26 00:01

John Starich