13. Let's Check Out!


#1

ok I know the answer is right and I finish this course part. But I don’t understand the result that i get
It gives me 5.5
but I think normally it must run the process so many times that every stock[key] must be 0 in the end so the result will be more than 5.5
or is the price for 6 bananas 4, i think 1 banana is 4?

MY 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 key in food:
if (stock[key] > 0):
total += prices[key]
stock[key] = stock[key] - 1
return total

print compute_bill(shopping_list)


#2

Hello @leeyann,

Lets understand what compute_bill() does.
It takes any list of fruits and checks if a fruit is in stock, If it’s in stock…
It adds price of fruit to the total and as you buy one fruit so it subtracts 1 from stock number of the fruit.

Now if you see the last line…

print compute_bill(shopping_list)

You’re calling compute_bill() function with an argument of shopping_list

shopping_list = [“banana”, “orange”, “apple”]

Is Banana is in stock ? Yes! 4.0
Is Orange is in stock? Yes! 1.5
Is Apple is in stock? No! 0

so total is 4.0 + 1.5 + 0 = 5.5.

I hope it helps you!


#3

@greentreemee

Ah ok so the function just test: is my shopping_list product inside in stock and is it bigger as 0.
Ah now I realize also was stock mean. It mean how much of this product are in the supermarket.
Now I understand also while I definite stock[key] - 1

I think all the time stock will be how often I will buy the product.
Thanks now I understand the full content

this was a language transfer mistake,


#4

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