Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dictionnary Python variable

I want use a variable as dictionnary name for find and use fastly some values. (For example i want the agressivity of my fighter one). I use a variable because the fighter can change and i want a dynamic script.

fighter1 = {"agressivity": 6, "agility": 2}
agressivity = 0
arenafighter = "fighter1"
agressivity = arenafighter["agressivity"]

But i have the same problem "TypeError : string indices must be integers"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use a dict in a dict

fighters = {
    "fighter1": {"agressivity": 6, "agility": 2},
    "fighter2": {"agressivity": 8, "agility": 4},
}

arenafighter = "fighter1"
agressivity = fighters[arenafighter]["agressivity"]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading