What's wrong in this code?


#1

prices={"banana":4, "apple":2, "orange":1.5, "pear":3}
stock={ "banana":6, "apple":0, "orange":32, "pear":15 }

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

Can someone help me to understand what's wrong in this code.


#2

here:

print "price:","%s"%prices[price]

why do you use a comma? the great thing about %s is that we can use it as placehodler for variable so we have a single string:

"price: %s"

#3

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

To me it looks good but the code academy python editor says "Oops, try again. Check what your code prints for orange. It doesn't look quite right!"


#4

"price: %s" prints %s since it is part of the price string.


#5

but you shouldn't use a , in your print statements, it causes problems with the validiation


#6

Even with a comma(,) i am not getting any syntax error and the output i see as shown above. But the code academy python editor says "Oops, try again. Check what your code prints for orange. It doesn't look quite right!".

Just wanted to know if there is any error in the code.


#7

its syntax wise not wrong, but the exercise can't validate it correctly since comma's are argument seperators.


#8

Oh ok. Let me try without commas.


#9

Hey, I have the same problem, has you found the solution?


#10

Wrong approach, go here, make a new topic, fill in the template so we have all details needed to help you

asking and posting answers is a violation of the guidelines


#11

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