Keeping Track of the Produce


#1

Task:

Loop through each key in prices.
Like the example above, for each key, print out the key along with its price and stock information. Print the answer in the following format:

apple
price: 2
stock: 0
Like the example above, because you know that the prices and stock dictionary have the same keys, you can access the stock dictionary while you are looping through prices.

When you're printing, you can use the syntax from the example above.

My code:

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

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

for item in prices:
    if item == "apple":
        print item
        print "Price: %s" % prices[item]
        print "Stock: %s" % stock[item]
    elif item == "orange":
        print item
        print "Price: %s" % prices[item]
        print "Stock: %s" % stock[item]
    elif item == "banana":
        print item
        print "Price: %s" % prices[item]
        print "Stock: %s" % stock[item]
    elif item == "pear":
        print item
        print "Price: %s" % prices[item]
        print "Stock: %s" % stock[item]

Result after interpretating:

orange
Price: 1.5
Stock: 32
pear
Price: 3
Stock: 15
banana
Price: 4
Stock: 6
apple
Price: 2
Stock: 0
None

ERROR:

Oops, try again. You should have exactly three print statements! (Make sure there are no occurrences of 'print' in any comment or string!)


#2

Oh boy, don't you think that's a little too much work for repeating the print statements? The loop will go through all the fruits. Your print statements between the if statements are exactly the same so there's no need for different conditions if your outputting the same thing regardless.

Also, remember to print exactly what they want as in

apple
price: 2
stock: 0

and not

apple
Price: 2
Stock: 0

#3

I still get the error though.
prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear": 3
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear": 15
}
for key in prices:
print "%s" % key
print "prices: %s" % prices[key]
print "stock: %s" % stock[key]


#4

This is what my output looks like:

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


#5

Your key is already in a string. No need to string it again in print "%s" % key. Just print key is enough.


#6

still the same error : I replaced : print "%s" % key with print key


#7

Are they asking for the order or something?


#8

They said to print

apple
price: 2
stock: 0

You are printing

apple
prices: 2
stock: 0

#9

■■■■! thanks:D. haha


#10

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