8. Keeping Track of the Produce


#1



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

orange
price: 1.5
stock: 32
pear
price: 3
stock: 15
banana
price: 4
stock: 6
apple
price: 2
stock: 0
None

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

thanks.


#2

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.


#3

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
orange
Price:1.5
Stock:32
pear
Price:3
Stock:15
banana
Price:4
Stock:6
apple
Price:2
Stock:0
None


#4

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.


#5

ah, that's irritating. Thank you


#6

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


#7

ahaaaa , thanks alot :+1:


#8

How annoying! Thank you for the assistance!


#9

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!


#10

%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.


#11

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


#12

it should look like this

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


#13

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