14. Looping Over a Dictionary


#1



https://www.codecademy.com/en/courses/python-beginner-en-cxMGf/2/1

It looks like your code did not print "b berry".

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])


Looping over a dictionary
#2

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


#3

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


#4

blogmaster edited his post, this response is no longer valid


#5

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?


#6

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


#7

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.


#8

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.


#9

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


#10

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:


#11

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]


#12

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