Solution for learning python, lesson 5 number 8


#1

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:

apple
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:

orange
price: 1.5
stock: 32
pear
price: 3
stock: 15
banana
price: 4
stock: 6
apple
price: 2
stock: 0

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


#2

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]


#3

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