Stocking out... please help me


#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/courses/python-beginner-P5YtY/1/4?curriculum_id=4f89dab3d788890003000096
https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/stocking-out-?action=lesson_resume
<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 12 instead of the correct 7

<What do you expect to happen instead?>

```python

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

<do not remove the three backticks above>

https://www.codecademy.com/courses/python-beginner-P5YtY/1/4?curriculum_id=4f89dab3d788890003000096

#2

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

here is the indentation.


#3

Never mind that does not work


#4

you have total += price both inside and outside your if stock>0 statement.


#5

i removed the top one, and the bottom one at different times, and it still does not come out right.


#6

ok I ran through the code and managed to recreate your result and I’m pretty sure you have an indentation error. So first remove the total = total + prices[x] line above your if statement.
Then check the indentation of your if statement. The beginning of the if should not match the beginning of the for loop. it needs to be indented past the for loop, which means the lines inside your if will also need to be indented more.
Now check where your return statement is. It shouldn’t be inside the if statement or the for loop. the indentation of the return statement needs to line up with the beginning of the for loop


#7

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