Error Code in "Keeping Track of Produce"


#1

I would like to think I am doing this correctly, but I may not be. I don't know how to print the key required in the first print. I know I have to print the key from each line to have it come up on the console. Please help.


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


#2

I have solved my issue.


#3

How did you solve your issue?


#4

i think he just removed the parentheses in the first print statement.


#5

Hi its his for loop

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

Inside the [ ] you should put price and print price like that

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

`


#6

@jakeyj3, @terablaster75289

The error is in this line:

He named each element price but calls key in the dictionaries:

change key to price and it works fine. Or just change price in the for-loop to key.


#7

Thanks so much, I couldn't believe I missed it.


#8

Hi,

i am unable to find what's wrong in below code...

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

When i ran this , it prints the output exactly .. but giving below suggestion and not letting me go forwards.

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


#9

Hi here

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

Remove the uppercase P and S to price:and stock:


#10

Hi wizmarco,

Thanks for your reply..

The uppercase p and s in "Price: %s" & "Stock: %s" are just a string that we want to print on console .. but actuall value is getting from % prices[each] & % stock[each]


#11

You have to exactly do as the instructions say, which is lowercase price and stock, otherwise the exercise won't let you pass


#12

yes.. i have changed it to lower case..and able to go forward..

Thanks alot


#13

Trust me, wizmarco is never wrong :wink:


#14

yes.. you are right..

Thanks to you all


#15

it still doesnt solve the issue for me...it says "Oops, try again. Check what your code prints for orange. It doesn't look quite right"


#16

Hi can you post your code.


#17

prices = {"banana": 4,"apple": 2,"orange": 1,"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]


#18

Remove the space before the ":"

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

#19

thanx alot..it works!!!but i dont understand how..


#20

Just do exactly as the instructions tell you, your code wasn't wrong, but it wasn't in the format the instructions told you :wink: