Keeping Track of the Produce


#1


Keeping Track of the Produce


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:%f"%prices[key]
    print "stock:%d"%stock[key]


orange
price:1.500000
stock:32
pear
price:3.000000
stock:15
banana
price:4.000000
stock:6
apple
price:2.000000
stock:0
None

what is wrong? in my code

OK Got it %s for both price and stock and space between value and name


#2

%s for both price and stock and space between value and name


#3

you code should look like

apple
price: 2 #see the space between : and 2 
stock: 0  #same here

and
%f is the format code for a float.try to figure it out what you need there


#4

thank you for your reply


#5

Dear all,
I need someone to check the code below....the results looks correct to me, but system replies that something is wrong with oranges...

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]

P.S: indents are there, they just didn't get copied


#6

the problem is that instead of
prices = {"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3}

put,

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
you forgot to indent { and }


#7

It is a dictionary definition, so indentation with the braces is not important here.
However capitalization in your output is important.


#8

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