8. Keeping Track of the Produce


8. Keeping Track of the Produce

it's keeping giving me :
"Oops, try again. Check what your code prints for orange. It doesn't look quite right!"

what is the wrong with my code ?

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]

the output

price: 1.5
stock: 32
price: 3
stock: 15
price: 4
stock: 6
price: 2
stock: 0

and i wanna know why the output starts with orange key not banana??



The problem is with your print statements, there is an extra space after the %s before the quotation mark.

As for why it starts with orange, dictionaries are unordered. So even though you put them in as banana, apple, orange, pear. That isn't how python is actually storing them. I do not know the theoretical side of it, but just remember that dictionaries are unordered and do not store items in the order they are entered.


I'm having the same issue however mine doesn't and never had the space between %s and the quote heres my code and I have the same error message. Any idea?

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15

for fruit in prices:
print fruit
print "Price: %s" % prices[fruit]
print "Stock: %s" % stock[fruit]

It prints it out correctly but still gives me the error. Heres the print


If you notice the formatting in the example of how they want the code, price and stock are all lowercase. Try changing your code to match their output.


ah, that's irritating. Thank you


Your welcome! Sometimes the formatting pickiness can be a little frustrating, but you've gotta do what you have to do.


ahaaaa , thanks alot :+1:


How annoying! Thank you for the assistance!


Does someone know why the prices and stock numbers are interpreted as strings here?

My intuition was to print

print "price: %f" % prices[produce]

That gave the correct results, too, and I'm guessing it wasn't accepted because it didn't match the format they wanted. But why can the numbers be interpreted as strings here in the first place?

Many thanks in advance!


%f, %d, and %s each tell "print" how to format the output and some only take certain inputs. For example, %f will not take a string because it can't convert that to a float. However, %s doesn't necessarily care what type of character is inputted, it'll just format it as a string. If you use the type function you will see that the prices and stock levels are being stored as int's or floats.


Wow, that was quick. And it makes complete sense.
Learned something new. Thanks a lot, mkordik!


it should look like this

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


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