Python Clarification - Something of Value


#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/something-of-value-?action=lesson_resume

<In what way does your code behave incorrectly? Include ALL error messages.>

No error, just trying to figure out how to have a format of

apple
price
stock
$ of inventory

<What do you expect to happen instead?>
List of the price of the inventory for each item. It lists the same numbers after each item and it seems to only pertain to the first item in the string.

```python
price = {

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

for key in price:
print key
print “price: %s” % price[key]
print “stock: %s” % stock[key]

total = 0

for key in price:
total += price[key] * stock[key]
print total

<do not remove the three backticks above>

#2
for key in prices:

Be sure to refer to the dictionary.


#3

Still a little confused. Are you suggesting changing my syntax from price to prices? I am passing the lesson, I just am trying to figure out why I am not getting the correct prices after the items.


#4

Yes. There is no price object. Iterate over the prices dictionary and everything will work as expected.


#5

If I understand you correctly:

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]

total = 0

for key in prices:
total += prices[key] * stock[key]
print total

show this on the console:

orange
prices: 1.5
stock: 32
48.0
93.0
117.0
117.0
pear
prices: 3
stock: 15
48.0
93.0
117.0
117.0
banana
prices: 4
stock: 6
48.0
93.0
117.0
117.0
apple
prices: 2
stock: 0
48.0
93.0
117.0
117.0


#6

Use price in the label.


#7

I changed that but let me reiterate my issue isn’t passing the lesson, it is getting my output to reflect the inventory. Maybe I am misunderstanding this concept.


#8

Be sure that none of the above in inside the loop above it. Without seeing formatted code we cannot tell where the indentation issues are.


#9

ahhh I see. So that changed the output from printing a string of numbers after each item to just after the last item:

apple
price: 2
stock: 0
48.0
93.0
117.0
117.0

I guess my confusion is that isn’t the string of numbers supposed to be
48
45
24
0
as that would make more sense as to the cost of your inventory.


#10

Your first loop should look like the following (blurred to prevent others from copying):

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

The above will output 12 lines.

The second loop should look like this:

total = 0
for item in prices:
    total += prices[item] * stock[item]
print total

The above will output one value.


#11

Thank you very much for your help, I am realizing I need to pay more attention to my indentation! I will go back and redo the into to python to make sure I refresh the basics. Again, I appreciate your guidance!


#12

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