Help with Stocking Out


#1



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


What do I need to do?

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: local variable 'stock' referenced before assignment


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


#2

Maybe try assigning a value of zero to stock before you begin your loop.


#3

That caused this error:

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7


#4

You are very close. Look at your for loop. Since you are iterating through each "item" you need to add [item] after stock in your if statement and after stock in your counter (only the left side of the equation).


#5

Keep in mind that stock is a dictionary, too.

stock[item]

#6

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

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: 'int' object has no attribute 'getitem'


#7

Missed one stock, should be as the others, stock[item].

That line is not needed.


#8

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