Why doesn't the if statement work?


#1

My code seems to behave just as it did before I added the if statement, I can't seem to understand what I did wrong.

It says "calling compute_bill with a list containing 1 pear, 1 orange and 7 bananas resulted in 32.5 instead of the correct 28.5"

Shouldn't it not add the price to the total if the item has a stock greater than zero?

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

#2

needs to bestock[item] -= 1


#3

Hmm I just changed that and got rid of the print line and it says I completed the lesson, still not quite sure what's going on.


#4

One of the lessons states explicitly to not print anything. This could be that lesson.


#5

I tested it with -= and with just - , as long as the print statement is not preset it will work. BUT as @elweyese99 said, generally use -= or += to -/+ to the variable itself.


#6

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