Whats wrong with my code


#1



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

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


#2

me too, I got the same error information for my code...


#3

So, I ran the code in the Python IDLE, and got the following output:

orange
prices: 1
stock: 32
banana
prices: 4
stock: 6
apple
prices: 2
stock: 0
pear
prices: 3
stock: 15

So the issue it's having is, it's saying that oranges cost $1.00 and not $1.50. I haven't done this particular exercise yet, so I'm not sure if you were shown to use them, but you could use %f instead of %d. For instance, %0.2f will print everything with 2 decimal places, so a 1 will be 1.00 and your oranges will come out to be 1.50. Hope this helps!


#4

Okay, so I just did that program and went at it a slightly different way than you did.

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

for item in prices.keys():
print(item)
print("price: " + str(prices[item]))
print("stock: " + str(stock[item]))


#5

I ran into the same problem you did, except I used %.1f and %i. But apparently it wants %s for both.


#6

change your code in printing section to
prices ------> price

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