Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAtang Lekang
2,509 PointsHow do you write the a for loop that iterates over the provided dictionary.
The body of the loop should have two print statements: one to print the key and one to print the value of the current item
pets = {'name':'Ernie', 'animal':'dog', 'breed':'Pug', 'age':2}
for item in pets:
print(item)
print(pets.item())
2 Answers
Aaron Jorgensen
Python Development Techdegree Graduate 13,658 Pointspets = {'name':'Ernie', 'animal':'dog', 'breed':'Pug', 'age':2}
for key, value in pets.items():
print(key, value)
Steven Parker
231,269 PointsThere are actually several ways to accomplish this objective, and you're getting close to one potential alternative. But even though the instructions don't mention it, this challenge specifically wants you to use the ".items()
" method in your loop iterable.
The advantage to that approach is that it will return both the key and value at the same time, making both "print" statements very simple.