8. Keeping Track of the Produce


#1

Code
https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/1/3?curriculum_id=4f89dab3d788890003000096

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

Output

prices: 1.5
stock: 32
prices: 3
stock: 15
prices: 4
stock: 6
prices: 2
stock: 0
None

Problem #1

My understanding is that a dictionary has the following structure -> dictionary_name = {"key":value}. When I run the following for loop:

for key in prices:
print prices[key]

The output is the following values:

1.5
3
4
2
None

First problem: how do I know what variable is being called in the for loop (e.g. for key in prices:)?

Problem #2

Because of Problem #1 I don't know how to print the dictionary keys and values in the following format:

apple
price: 2
stock: 0

Slightly frustrated.

Melissa


#2

Hi, Melissa!

1) What I think your saying is how do you know what variable is being called. Key (or item) just stands for an item in the dictionary. Prices is the dictionary it loop in. So, the code says (this is in pseudo code) Print every key in Prices. I hope this answers your first question. :smiley:

2) So know you need to use the %s. First, you have to print the item. Then use te %s. For example:

"You have %s %s in stock" % key, prices[key]

So it should turn out like this:

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

Hope this helps
~Candy


#3

Thank you! That was very helpful. I was getting hung up on the name of the dictionary and missed the basics of simply calling a string with price and stock (I was thinking too hard about the dictionary).

Regards,

Melissa


#4

Printing a string not calling a string :).

Melissa


#5

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

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

I encounter the following error message: "Check what your code prints for orange. it doesn't look quite right"

Any thought?


#6

solved in the meanwhile


#7

I've got the same error b1nik. How did you solve it?


#8

I've had already the same error, any help??


#9

It might be sensitive. Instead of prices do price when you do "price: %s"


#10

Thanks candycornlover1 - this exercise is very sensitive to formatting errors:

This is the correct format:

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


#11

Hi,

Here is the 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]

total = 0
for i in prices:
    total=total+(prices[i]*stock[i])
print total

"


#12

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

for key in price:
if key=="apple":
print key
print "price:%s"%price[str(key)]
print "stock:%s"%stock[str(key)]

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

what's the problem


#13

I have a doubt in this code,please help! The question I want to ask is--Why are we taking %s for printing price and stock values,shouldn't it be %d?


#14

What does %d mean anyway? But look at this website for information on %s:


#15

I have been struggling with this too.

I did a Google search for "what does %s mean in python" and got this:

"....The d in %d stands for decimal. %d is for formatting numbers. %s is for formatting strings..."


#16

prices={
"banana":4,
"apple":2,
"orange":1.5,
"pear":3
}
stock={
"banana":6,
"apple":0,
"orange":32,
"pear":15
}
for key in prices:
if key=="apple":
print key
print "price:%s" % prices[key]
print "stock:%s" % stock[key]

what's the problem with this code?


#17

have you figured out this? I encounter the same problem


#18

There should be a space before %s


#19

prices={
"banana":4,
"apple":2,
"orange":1.5,
"pear":3
}
stock={
"banana":6,
"apple":0,
"orange":32,
"pear":15
}
for key in prices:
if key=="apple":
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]

like this? it's still not working ,it said check what your code prints for orange


#20

Don't need if key == "apple":
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]