1

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)
Taux1c
  • 65
  • 1
  • 1
  • 7
  • I should mention I tried print(name), print(level[name]), and even print(level)[level][name]) – Taux1c Feb 07 '20 at 18:29
  • Please [edit] your question. What is your current output? What do you want? – Jongware Feb 07 '20 at 18:29
  • 1
    Does this answer your question? [Iterating over dictionaries using 'for' loops](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops) – Patrick Haugh Feb 07 '20 at 18:31

1 Answers1

1

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
Lapis Rose
  • 644
  • 3
  • 15
  • Yes! Thank you! I have been working on this for hours! I don't fully understand it but it does what I am looking for! I am going to have to study it for a bit! I will accept this as soon as the 5 min timer is up! – Taux1c Feb 07 '20 at 18:37
  • Ah one thing that I forgot about python dictionaries is that iterating through them with ```for level in levels``` implicitly calls ```for level in levels.keys()``` because they implement a special iterator. The only problem in your code is that you need to wrap ```name``` with quotations. – Lapis Rose Feb 07 '20 at 18:40
  • Oh wow! I was trying to figure out what the .keys() was for. Not too bad for self taught (I think) I read the w3schools tutorial but it didn't go this in depth. I can't thank you enough! I have been working on that for like 3 hours and no luck with research on it so I decided to post it. – Taux1c Feb 07 '20 at 18:43
  • I am going to add the code I am translating it into (have translated) into the question. It works perfectly! – Taux1c Feb 07 '20 at 18:52