8.keeping track of the produce


#1

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

for fruits in prices :
print fruits
print "prices: %s" % prices[fruits]
print "stock: %s" % stock[fruits]

Oops, try again. Check what your code prints for orange. It doesn't look quite right!

I don't understand what I did wrong

Thanks for your help


#2

Hi this line

print "prices: %s" % prices[fruits]

this part

print "prices: ...

just remove the s to price


#3

Sorry ! I don't understand. Why do we put a "s" at prices at the beginning and after we remove it ? Thanks for your help!


#4

They aske you to put the s to price when you made the dictionary but now they are asking you to print that

apple
price: 2
stock: 0

with that code

print "price: %s" % prices[fruits]
print "stock: %s" % stock[fruits]

but if you put the s to price you will have as output

apple
prices: 2
stock: 0

and it will see it like a error


#5

Just a small mistake and compiler went out of control....hahaha ..btw thanks for the help


#6

This worked for me:

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

But did you understand why? I did not. Can someone explain to me, why do I have a dictionary called price**s**, but in the loop I say price - without "s"

Can someone put this into words, please?: print "price: %s" % stock[item]

Thanks!


#7

right, I think I finally grasped it. so here's how I would put it into words:

i call everything that is in stock as "item", and starting a loop that will go through each item that is in the dictionary called "stock" and I want to see:
item name
word price then item's actual price
word stock then actual number of items in stock

for item in stock:
show item name
show word "price" then take an actual price of that item
show word "stock" then take an actual stock of that item

If this is wrong, please correct me or if it is right then I hope this will help someone


#8

Actually for a real python compiler...there can be any print statement but the compiler here is specifically deigned for the output codecademy desires.. Since the question in the left asks to print price: instead of prices: ....so the error is coming.. Although you won't face any such error in a real-time python programming


#9

It is really weird because you have to use "key" instead of fruit. Don't know why. :confounded:


#10
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]

What is wrong in this code?


#11

You should put spaces before

=
sign. There is also
'
after prices[key]

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]


#12

none of you answered the actual question to the original error... I have the same problem, and it is not the syntax of price as I have that, the issue is the terminal is outputting 2 for orange and not a float of 1.5.. here is my code, any help works, thanks in advance.
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[key]
print "stock: %s" % stock[key]


#13

nevermind I found it, the issue was my key call, changed it to fruit and boom.


#14

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

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

for i in prices:
print i
print "price: %s" % str(prices[i])
print "stock: %s" % str(stock[i])


#15

my code
prices = {"banana" : 4, "apple" : 2, "orange" : 1.5, "pear" : 3}
stock = {"banana" : 6, "apple" : 0, "orange" : 32, "pear" : 15}

for i in prices:
print i
print "price: %s " % float(prices[i])
print "stock: %s " %(stock[i])

I think all my code is correct as its printing what it has to but still it gives me "Check what your code prints for orange"

I tried to previously suggested approach but they didn't worked.

Thanks in Advance


#16

Hi these lines

print "price: %s " % float(prices[i])
print "stock: %s " %(stock[i])

remove the space after %s like that

print "price: %s" % float(prices[i])
print "stock: %s" %(stock[i])

#17

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