2. This Is Key


#1

i had a question about this....

A simple dictionary

d = {"foo" : "bar"}

for key in d:
print d[key] # prints "bar"

why does this print out "bar"
i thought the key = "foo"
and the value = "bar"


#2

The key does equal "foo", but you don't print the key. d[key] is the value that d has for key.

When key is "foo" d[key] is "bar".

To print out the key just print key.


#3

ok. i think i get it now. thank you!


#4

I'm having some trouble with this. here is my code:

for key in webster:
print webster[key]

but it comes back with

Oops, try again. Your code looks a bit off. Check the Hint if you need help! Your code threw the following error: string indices must be integers, not str

I tried the code as

for key in webster:
print webster

this allows me to pass the lesson but doesn't print out the keys it just spells out dab in brackets. I also tried just printing key and that just gives me letters.


#5

What is webster here? Can you show us all your code?


#6

This seams to work fine.

for definitions in webster:
print webster[definitions]


#7

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."
}

Add your code below!

for key in webster:
print webster[key]

May help you

#8

here code for who hard to understand how to print all in one function, u can print one by one until all getting print

 # Add your code below!
    for x in webster:
        print webster["Aardvark"]
        print webster["Baa"]
        print webster["Carpet"]
        print webster["Dab"]