A day at the super market-Keeping track of the produce


#1

Hello friends,
I am strucked in the the Keeping track of the produce exercise. It says Oops, try again. Check what your code prints for orange. It doesn't look quite right!. Can anyone help me out?

 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]

#2

I had the same problem. What fixed it for me was putting the printed phrases in lower-case, but yours is almost the same. Try just copying mine.

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]


#3

Your code and my code almost the same. Think it's not wrong with the python syntax of mine. I just copied yours and it worked. Couldn't figure out why it hasn't worked of my code. Thanks


#4

The error was here:

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

we have to put "price" after print instead of "prices":

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

I had the same error.


#5

oh ok. Thanks. However, It should not have shown error, basically it's a text that we are going to print either price or prices.


#6

I completely agree. The error message wasn't helpful in trying to debug the code.


#7

Oh, I met the same problem, although I put "price", not "prices" after print. I don't know why.

And anybody knows how it works with order, why the printed order is not "banana" - "apple" - "orange" - "pear" but "orange" - "bear" - "banana" - "apple"?


#8

Hi the correct code is there:
price = {
"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 "price: %s" % price[key]
print "stock: %s" % stock[key], "\n"


#9

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] ,

Same thing happens to me, wtf?


#10

Instead of writing:
print "%s" %key
print "prices:%s" % prices[key]
print "stock:%s" % stock[key]

Write it like this:
print "%s" % key
print "price:%s" % prices[key]
print "stock:%s" % stock[key]


#11

following format:
apple
price: 2
stock: 0

your code was "prices:%s", the 's' character is not require


#12

yeah, I have already corrected it and moved on to further exercises


#13

for p in prices:
print p
print 'price: %s' % prices[p]
print 'stock: %s' % stock[p]

my question is as prices[p] is int or float,Why it is replaced in place of a string(%s)?


#14

Hii,
this >>>> "%s"<<< is standing for what, i don't get it.


#15

@gwakisa:
Look at this example:

amount = 3.5
print "total pay: %d$" %amount
print "total pay: %f$" %amount
print "total pay: %s$" %amount

This is the printed result:

total pay: 3$
total pay: 3.500000$
total pay: 3.5$
None

use "%s" to format the printed value as a string.
use "%d" to format the printed value as an integer.
use "%f" to formate the printed value as a float.

Hope it can help.


#17

solution in here

point is
"price: %s" are correct but
"price : %s" are not correct


#18

thanks a lot, it was indeed the problem. My working code is:


    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]

#21