Taux1c
Taux1c

Reputation: 65

Python3 Nested Dictionary print

Okay so I for the life of me haven't been able to figure out hot to make this snippet work. My thinking is that since we are working with level in levels we are already in the first dictionary so it should be level[second dictionary]

My current output is an error name not defined. What I am looking to do is to be able to print the name from each dictionary, the hoist value, etc. Ultimately it I will be querying these into statements. Such as print(level[colour]) should print the colour of the current level in a for statement or print(levels[Admin][colour]) should output the colour of admin.

levels={"Admin":{"name":"Admin","hoist":"1","colour":"red"},"Moderator":{"name":"Moderator","hoist":"1","colour":"yellow"},"Henchman":{"name":"Henchman","hoist":"1","colour":"yellow"},"Member":{"name":"Member","hoist":"0","colour":"green"},"Verify":{"name":"Verify","hoist":"1","colour":"white"},"Leach":{"name":"Leach","hoist":"1","colour":"pink"}}



for level in levels:
    print(level[name])

Any help is appreciated.

Here is the syntax I am using it in.

@client.command()
async def roles(ctx):
    guild=ctx.guild
    for level in levels.keys():
        name=levels[level]['name']
        hoist=levels[level]['hoist']
        colour=levels[level]['colour']
        await guild.create_role(name=name,hoist=hoist)

Upvotes: 1

Views: 34

Answers (1)

Lapis Rose
Lapis Rose

Reputation: 644

Your keys are all strings, so you need to wrap name with quotations.

for level in levels:
    print(levels[level]['name'])

print(levels['Admin']['colour'])

Output:

Admin
Moderator
Henchman
Member
Verify
Leach

red

Upvotes: 1

Related Questions