This is KEY!


#1

Instructions: Use a for loop to go through the webster dictionary and print out all of the definitions.

webster = {
"Aardvark" : "A star of a popular children's cartoon show.",
"Baa" : "The sound a goat makes.",
"Carpet": "Goes on the floor.",
"Dab": "A small amount."
}

Here is my code:

for name in webster:
print webster["Aardvark"]
for name in webster:
print webster["Baa"]
for name in webster:
print webster["Carpet"]
for name in webster:
print webster["Dab"]

My code worked but I was wondering if there is an easier/shorter way to write this Loop. Any thoughts would be greatly appreciated


#2

Yes, there is a better way to write the code.
You only need to call the for loop once, then write print webster[name], like this:

for name in webster:
    print webster[name]

This will print out each definition in the dictionary. If you're using the for loop but still manually writing the names for the definitions, the for loop becomes useless. Also, don't use four for loops. You only need one here.


#3

Thank you for your help!


#4

No problem! It would really help me if you'd mark the answer as the solution :slight_smile:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.