14. Looping Over a Dictionary



<Below this line, add a link to the EXACT exercise that you are stuck at.>
<In what way does your code behave incorrectly? Include ALL error messages.>
It looks like your code did not print “b berry”.
<What do you expect to happen instead?>
I expect no error message, as it appears correct in the output console


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

for key in d:
# Your code here!
print “%s %s” %(key, d[key])

<do not remove the three backticks above>


Too many spaces between %s and %s on the last line. The resulted printed often should be an exact match to the required output


You can also use (correcting what I said before: “just need”) to print the key and it’s valeu:
print key, d[key]


blogmaster edited his post, this response is no longer valid


I’m not sure what I have to say to you about it.
What you saying is that my answer can make others to go to bad understanding?

But, anyway, I answered the sentence above because it was a surprise to me that isn’t necessary to use the ‘%’ operator. So I was intented to share my discovery.

But in resume, I shold not participate when I’m not sure about my answers?
I’m also learning here, stetim94, ok?


maybe I should not use the word ‘need’ - but this was because of my poor english.


And because you used the word need, it sort of implies that the using %s %s % (key, d[key]) isn’t correct. Using print key, d[key] as a alternative is good, but then the sentence would be formulated as: You can also use …

You are certainly allowed to share your view, which i encourage, but this also gives other people the right to disagree with your answer. Which i did, a little to harsh.


You are right. For the record, I changed the sentence above to avoid guide incorrectly.

And no problems with disagreement. I was just wondering what was so wrong in my firt sentence. But now I know: to take care with the word’s meaning.


Very good :slight_smile: I also edited my answer. But it is impossible for me to know your english isn’t good (english isn’t my native language either), so when i read need, i interpreter it as such. Good we could agree on the issue and you edited the post


Thank you, all the warm-hearted buddies in the post, who answered my questions.
the discussion helped me a lot!

Wish you and myself a great success on the way to coding :slight_smile:


Not sure, but I used a different solution:

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

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


Looks very similar to blogmaster36220 solution. Both methods work fine :slight_smile: