Stocking Out question


#1



I am not exactly sure what the difference between these two peices of code are... I though that they both end up with the stock item being decremented by one if an item is bought... the top piece of code popped up as incorrect and said it arrived at an incorrect number.


Replace this line with your code. 
def compute_bill(food):
    total = 0
    for item in food:
        if stock[item] > 0:
            total += prices[item]
            stock[item] - 1
    return total


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


#2

Suppose a variable.. (say x)

x = 10
# and we do this..
x -= 1 # This assigns a new value to our variable x
print x # 9

Take another example..

x = 10

# and we do this..

x - 1 # this statement have no effect on value of x

print x # 10

In first code,
stock[item] - 1 does not changes the stock dictionary.

Note:
-= is a compound assignment operator
x = x - 4 can be written as x -= 4


#3

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