My function compute_bill(food) doesn't work


#1



Stocking Out


Oops, try again. calling compute_bill with a list containing 2 apples, 3 oranges and 8 bananas resulted in 36.5 instead of the correct 28.5


a correct result


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


#2

Err.. I'm a bit unsure of what exercise you're on. I guess you're on "Let's check out". You should check out this condition.

You could try checking to see if the item in stock is more than 0 in value rather.

        if stock[item] > 0:

Then, fix code as appropriate. :slight_smile:


#3

Your code will not pass the SCT because they asked you to define only one variable and that is total whereas you have count and total . You do not require two variables.

You can just change all references to count into references to total,

to,

total += stock[item]

and this line is incorrect,

because you are just supposed to subtract one from the stock's item not store it in a variable,

stock[item] -= 1


#4

Thanks
I'd some errors there. fixed it and passed.


#5

Thanks for the hints.

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


#6

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