Solution for learning python, lesson 5 number 8


Those are the instructions:

Loop through each key in prices.

Like the example above, for each key, print out the key along with its price and stock information. Print the answer in the following format:

price: 2
stock: 0
Like the example above, because you know that the prices and stock dictionary have the same keys, you can access the stock dictionary while you are looping through prices.

When you’re printing, you can use the syntax from the example above.

My code is:

prices = {
“banana”: 4,
“apple”: 2,
“orange”: 1.5,
“pear”: 3

stock = {
“banana”: 6,
“apple”: 0,
“orange”: 32,
“pear”: 15


for key in prices:
print key
print “price:”, prices[key]
print “stock:”, stock[key]

The output of my code is:

price: 1.5
stock: 32
price: 3
stock: 15
price: 4
stock: 6
price: 2
stock: 0

codeacademy doesnt accept my answer because it says it doesnt look quite right.


print "price:", prices[key]

The lesson expects a string printing value. so you need to use string formatting syntax ( %s ) here. so your code should be like

print 'price: %s' % prices[key]


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