12/13 - Stock list updated?


#1

Hi,
I have finished this exerices, however when I print the stock list it does not appear to be updated:
Here is the code:

shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!
def compute_bill(food):
    total = 0
    
    for item in food:
        if stock[item] > 0:
            total += prices[item]
            stock[item] -= 1
    return total

compute_bill(shopping_list)
print stock


Answer:
{'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0}
{'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0}
None

Note the stock list has not actually been reduced from our purchases... why?


#2

That's because you're not calling "compute_bill()" before the "print stock", so of course the inventory hasn't changed!

Let's go shopping first and see how it works!

Insert this between the function and the "print stock":

compute_bill(shopping_list)

You'll see the values go down one each for banana and orange and apple will still stay at 0.


#3

Perfect, thanks :smile:

I see that its because we had only defined the compute_bill function (and not actually called it on the shopping_list).. (right?)

(also, as an aside: the dictionary doesn't actually get 'permanently' updated with new (reduced stock) values either right? - only if we call the stock{} dictionary again under this will the reduced values be used; its reset to the initial value every time we run the entire block of code (I think)).


#4

That's right, you need to call it to bring down the inventory!

Also, if you call the function several times you'll set the values go down and the data would be persistent between calls. The stock is only reset if you terminate the session and re-run the test. You can see that by running the compute and print several times:

print stock

compute_bill(shopping_list)

print stock

compute_bill(shopping_list)

print stock

compute_bill(shopping_list)

print stock


#5

Got it. Cheers Cody :smile:


#6

:frowning:
Error:
Oops, try again. Stock doesn't look quite right! Make sure to not call compute_bill since it changes the stock! It should contain: {'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0}


#7

murgendp: thats the initial stock list quantities. I wanted to know how they decrease every time we go shopping (i.e. every time we call the compute_bill function on our shopping_list).
(which is answered by codyroche above)..


#8

it tells you there don't call the function in the test window


#9

for stock[item] -= 1, why should we code"-" then"="?


#10

I'm having the same issue

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

File "python", line 24
return total
^
IndentationError: unindent does not match any outer indentation level

could someone help pls ??


#11

I am pretty certain that

stock[item] -= 1

is the same as saying

stock[item] = stock[item] - 1

its a short cut 'feature' for arithmetic.


#13

got it man , thanx
this is what actaully worked for me

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

it was just indentation