Making a Purchase


#1

I am doing in following way but facing error.

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 item in food:
total=total+food[item]
return total

print (compute_bill(prices))


#2

Hi,

compute_bill(food) should take a list of items that are in your stock and return the total price.

You wrote your code like it's supposed to be called on a dictionary. You are not supposed to compute what it costs to buy one of every item you got in stock, but rather to compute what it costs to buy one of every item in a given list like ["apple", "banana"].


#3

this code work for me:

def compute_bill(food):
total = 0
for item in food:
total += prices[item]
return total


#6


#7

How does the def compute_bill(food): know that it is supposed to run on the items in the shopping_list ?

Can someone explain how the function knows to use the list?


#8

It dosn't know that, but you can tell it to run on shopping list.

def double(number):
    return 2* number
    # the function doesn't know what number it will get

print double(5)

# this will print 10 because 5 was passed to the double() function

#9

Would the function have to be named to get called?

def_compute_bill(shopping_list)


#10

Try this -

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 item in food:
for key in prices:
if (item==key):
total+=prices[key]

return total

compute_bill(shopping_list)

Have a great day !


#11

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 item in food:
        total += prices[item]
    return total

This works but food is argument with no value, food is nothing, how can for loop iterate something that has no value...

this stuff is teaching people wrong or I'm not getting something


#12

hello buddies
this is the the right code


#13

if u want u can add the prices of "banana"+"orange"+"apple"=4+2+1.5==7.5


#14

The function takes one argument, in this case "food". So when you write the function, it would be looping over nothing BUT when you call the function at the end "print compute_bill(shopping_list)" you are required to pass it an argument, in this case it's shopping_list. So that list, shopping_list, is passed into the function and essentially python goes "food = shopping_list" and then runs the loop (from what I understand).


#15

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 prices:
total += prices[item]
return total

I am getting the error: Oops, try again. compute_bill(['apple']) returned 10.5 instead of 2

I don't understand why?


#16

Your compute_bill function adds up all the prices in the prices dictionary. The exercise asks you to add up all the prices of the items in a list given as a parameter.

E.g. compute_bill(["banana"]) will have the same result as compute_bill(["banana", "apple"]) because your functions behaviour is independent of the food parameter.


#17

thanks, it works for me