0

I'm making a basic dungeon simulator in Python, and I want to know how to work with the variables I'm using. They aren't just like the normal variables; the structure looks like this:

weapons: {
    swordType: {
        daggers: {
            cardboard: {
                brokenCardboardDagger: {
                    damage: 1,
                    critDamage: 3,
                }
            }
        }
    }
}

(There are more swords and more materials, but I am just providing an outline as to what they look like in the code)

So, looking at this type of variable, how would I, for example, print the damage of a sword? Or, how would I concatenate it so that the line of code would look something like this?

print "Your sword's damage is" + [special variable code]

Thanks for the help.

MrEikono
  • 25
  • 1
  • 6
  • You'd probably want a weapons class, then several swordtype subclasses that inherit from weapons (like daggers, etc.) You can refine it more to have subclasses that inherit from the swordtype classes. Read up on that, especially if you plan for your game to get bigger. – Al.Sal Jul 01 '14 at 14:38
  • I don't really get what you're saying... Maybe make a Pastebin. Not saying that that's not a good idea, I just don't know what you mean. – MrEikono Jul 01 '14 at 19:11

1 Answers1

2

They are perfectly normal; you have nested dictionaries. Use as many indexes as needed:

weapons['swordType']['daggers']['cardboard']['brokenCardboardDagger']['damage']

If you want to have that 'path' stored in a separate variable, use a loop to extract each subsequent nested value, or use reduce(). See Any Functional Programming method of traversing a nested dictionary?

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343