Making a purchase// a day a super market

#1

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

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

def compute_bill(food):
total = 0
for fruit in stock:
total += prices[fruit]

print compute_bill(prices)

help me to figure out``````

#2

why loop over `stock`? This function is going to calculate the cost of shopping for a customer, which we represent with a list of products (we supply this list as argument on function call)

also, your function has a parameter (`food`) we should use it

#3

I didn't get it ?
can you explain some more

#4

``````def compute_bill(food):
total = 0
for fruit in stock:
total += prices[fruit]

print compute_bill(['apple'])
print compute_bill(['apple', 'banana'])``````

so we have two customers (function calls) the first customer buys an apple, the second customer buys an apple and a banana

now the function should return the total of buying this products

hint: arguments supplied at function call will be passed into the function parameter

#5

compute_bill(['apple']) returned 10.5 instead of 2
shows this error

#6

i know, i only showed you what the arguments at function call will be. So you can fix your program.

now that you know what the arguments at function call will do, what do you need to do with the function parameter?

#7

letme see if I can get it. I am in working in totally different background. thanks for help

#8

I'm still not understanding why the compute_bill(['apple']) is returning 10.5.

#9

lol that's what I am saying

#10

because here:

``for fruit in stock:``

you loop over stock, which contains apple, pear, banana and orange.

we need to do something with the argument apple supplied at function call (`compute_bill(['apple'])`), how could we access the argument at function call? Something to do with the function parameter (`food`)

feel free to revisit exercises about parameters and arguments if you forgot how they work, or check external documentation

#11

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