14. Looping over a dictionary

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/python-beginner-en-cxMGf/2/1?curriculum_id=4f89dab3d788890003000096#

<In what way does your code behave incorrectly? Include ALL error messages.>
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.

<What do you expect to happen instead?>

```python

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

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

<do not remove the three backticks above>

In the result board:
b berry
None

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

2 Likes

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.

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.

4 Likes

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

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

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

4 Likes

Thanks! but still don’t work…

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

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