8. Keeping Track of The Produce


#1



Error message : Check what your code prints for orange. It doesn't look quite right!

It prints :
apple
price: 2
stock: 0
apple
price: 2
stock: 0
apple
price: 2
stock: 0
apple
price: 2
stock: 0
None


it should be just
apple
price: 2
stock: 0


prices = {"banana": 4, "apple": 2, "orange": 1.5, "pear": 3,}
stock = {"banana": 6, "apple": 0, "orange": 32, "pear": 15,}
for key in prices:
    key = "apple"
    print "%s" % key
    print "price: %s" % prices[key]
    print "stock: %s" % stock[key]


#2

First off, why do you have print "%s" % key? You should just print the key. print key Second, why are you setting key = "apple" in the for loop? Take that line out. If you are trying to only print apple, then there isn't a reason to use a for loop because you don't need to iterate the entire dictionary.


#3

Hi remove these lines

    key = "apple"
    print "%s" % key

#4

Thanks, now it is working just fine.


#5

I have the following code:

for key in prices:
    print "price: %s" % prices[key]
    print "stock: %s" % stock[key]

And I get the error:

Oops, try again. You should have exactly three print statements! (Make sure there are no occurrences of 'print' in any comment or string!)

What could be the problem?


#6

What is the rest of your code? What is the output?


#7

for key in price:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]

"print key" is missing in your code.


#8

Ah that was indeed the problem, thank you!


#9

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: %s" % prices[key]
print "Stock: %s" % stock[key]

why is the above not working. I get this response "Check what your code prints for orange. It doesn't look quite right!"


#10

HI these lines

print "Price: %s" % prices[key]
print "Stock: %s" % stock[key]

these parts

.."Price: %s" ..
.."Stock: %s"..

you should remove the uppercase P and S to price and stock


#11

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 "Prices : %s" % prices[key]
print "Stock : %s" % stock[key]

result : Oops, try again. Check what your code prints for orange. It doesn't look quite right!

output_to_console
orange
Prices : 1.5
Stock : 32
pear
Prices : 3
Stock : 15
banana
Prices : 4
Stock : 6
apple
Prices : 2
Stock : 0
None


#12

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: %s" % prices[key]
print "stock: %s" % stock[key]

that's what its suppose to look like..


#13

That worked. But why does it matter? It's a string that I'm printing. Why does uppercase matter?


#14

..Why does uppercase matter
well they want you to print

orange
prices: 1.5
stock: 32

which is different of

orange
Prices: 1.5
Stock: 32

#15

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