The code doesn't seem to work no matter what I do


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/en/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/stocking-out-

<In what way does your code behave incorrectly? Include ALL error messages.>
“calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 2 instead of the correct 7” I don’t know what this means, or where it got some of these numbers like 2 or 7, and it left out orange (in the list) in this error message, just generally confusing. And sometimes “your code looks a bit off, look at the console for help”

<What do you expect to happen instead?>
I expect for the code to work, as it’s instructing me. See if something is in stock, if it is, then deduct it’s stock by one, and add the price to the total, if not in stock, don’t add to total or subtract from stock.

```python shopping_list = ["banana", "orange", "apple"]

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

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

def compute_bill(food):
total = 0
for i in stock:
if stock[i] > 0:
stock[i] == stock[i] - 1
for i in food:
total = total + prices[i]
return total
elif stock[i] > 0:
return total

<do not remove the three backticks above> 

Indentations don't save correctly when posted^

#2

Screenshot (162)


#3

stock[i] == stock[i] - 1

that is a boolean expression. Use a single = to assign.


#4

same error, didn’t work


#5

What else does not look right? The return statements, perhaps?


#6

Oh nevermind, I failed so much at it that it gave me the answer. I called the wrong keys or something. Thanks anyway


#7
def compute_bill(food):
  total = 0
  for i in stock:
    if stock[i] > 0:
      stock[i] == stock[i] - 1
      for i in food:
        total = total + prices[i]
        return total
    elif stock[i] > 0:
      return total

Now compare to a working solution:

def compute_bill(food):
    total = 0
    for item in food:
        if stock[item] > 0:
            total += prices[item]
            stock{item] -= 1
    return total

Take the time to look critically at your code and be able to spot the errors, examine your reasoning when you wrote the code, and resolve yourself to the fact it is an error and why. This is important thought exercise when learning, so don’t cheat yourself of the experience, and make it a habit. You’ll thank yourself when you discover how much you are learning.


#8

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