12. Stocking Out


#1


Presented with the error: "Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in None instead of the correct 7"

What's wrong with this code?


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


#2

Did you mean to write, prices[key]?


#3

Thanks, but the code still does not work.

Error message: Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: global name 'key' is not defined


#4

If you look at these using different variable names: "item" in the loop and "key" in IF and the block of code, you must use the same variable name throughout the code.


#5

Changed my code to this:

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

I am presented with "Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 1.5 instead of the correct 7"


#6

That is the part that has complicated me, that someone enlightens us, lol


#7

Be sure to return total once the loop is complete, not in the first iteration.


#9

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