Confused

After fiddling around with the code I figured out the answer but I’m confused as to how that answer is correct.
Here are the directions:
Let's determine how much money you would make if you sold all of your food. 1.Create a variable called total and set it to zero. 2.Loop through the prices dictionaries. 3.For each key in prices, multiply the number in prices by the number in stock. Print that value into the console and then add it to total. 4.Finally, outside your loop, print total.
Here is my answer:
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 total in prices:
prices = 4 * 6
print prices`

Even though I didnt print out the total price for all the variables, it said that i was correct. If somebody could explain me how this is correct and how i can fix it. In the hints it said
You should print the number all by itself—no need for any additional text!
Does it want me to multiply each value/number individually? Isn’t there a way to make this easier?
Please help me understand this part. Thank you guys.

:1. Create a variable called total and set it to zero.

total = 0

:2. Loop through the prices dictionaries.

for item in prices:

:3. For each key in prices, multiply the number in prices by the number in stock. Print that value into the console and then add it to total.

    total += stock[item] * prices[item]

:4. Finally, outside your loop, print total.

print total
3 Likes

what does this symbol mean? +=
and thank you.

The += operator is a shortcut addition in place.

a += b   same as  a = a + b
1 Like

I keep getting this error on this line of code.
Oops, try again. compute_bill([‘apple’]) resulted in a TypeError: unsupported operand type(s) for +: ‘int’ and 'str’
Here is the code:
def compute_bill(food):
total = 0
for item in food:
total += item
return total
could it have something to do with the fact that the price of apple is 2 and there is 0 left in stock so when the system multiplies the two number it gets zero?

item is a string (all keys are strings). We need to refer to the value at that key in the prices dictionary.

total += prices[item]

ok i see. I was forcing the system to multiply a str by an int. Ok makes sense. I feel stupid. Thanks.

Be good to yourself. We all make mistakes. Only those who refuse to learn from them qualify for that label. You don’t need to put it on yourself.

2 Likes

no joke that actually made me feel better and even though i may get stuck basically on every exercise i’m gonna keep learning. Thank you very much for your words of kindness.

2 Likes

Seriously, I learn more when I make a mistake than when I get it right the first time. That way you know more about what to watch for in future errors.

I get really frustrated at this, too and that actually helped me as well. Thanks.

Thanks a lot mtf :slight_smile:

1 Like