14. Looping over a dictionary


#1



https://www.codecademy.com/courses/python-beginner-en-cxMGf/2/1?curriculum_id=4f89dab3d788890003000096#


After running the code it showed "b berry" in the result board, but under line appeared "Oops, try again. It looks like your code did not print "b berry". What was wrong in my code, please help to debug it.
Thank you.


d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}

for key in d:
    if d[key] == "berry":
        print key, d[key]# Your code here!

In the result board:
b berry
None


#2

Hi
could you try with print key+" "+d[key]?


#3

Thanks. But it didn't work.
It's the same as before.
Oops, try again. It looks like your code did not print "b berry".
Maybe there is something ignored.


#4

Hi @methodninja19907 and @thebelgian1 ,

The message is misleading.

You actually need to output all the keys and the associated values, not just the one with the key, 'b'.

Remove the if statement so that they all get output.


#5

Hi! I tryed but I didn't get it, and I'm stuck here


#6

hi @psjmsj ,
try
d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}

for key in d:
# Your code here!
print key, d[key]


#7

Thanks! but still don't work...


#8

I got it but doing in another way^^ thanks for all


#9

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