Making a Purchase


#1



Making a Purchase

Oops, try again. compute_bill(['apple']) resulted in a NameError: global name 'item' is not defined

I not sure whats wrong. For the example it had total += food but it said the same thing. I not using shopping_list because it a global variable? Why does total += food work. Also what is the difference between def thing = 0 and thing = 0. Thanks in advance.


Replace this line with your 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 food in prices:
         total += prices[item]
    return total


#2

Hi, @kentmjk ,

Design the loop header to access the food list, and assign successive values to a variable that will be used as a key to access the prices dictionary.

In the following loop, you are overwriting the original value of the food parameter of the compute_bill function ...

    for food in prices:
         total += prices[item]

If you would like to use the name, item, as a key to access the prices dictionary, set that up in the loop header.


#3

Write your code below!

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

ok it still coming up with a error. Oops, try again. compute_bill(['apple']) returned 10.5 instead of 2. I not really sure what you meant by overwriting the original value of the food parameter of the compute_bill function. But i renamed the key to item.


#4

Hi, @kentmjk ,

Your original function header that defined food as the parameter, as follows, was fine ...

def compute_bill(food):

You can restore it, as above.

Then, note that your current for loop header is ...

for item in prices:

Instead of iterating through the prices dictionary, iterate though the food list, as follows ...

for item in food:

That will enable the loop within the function to iterate through whatever list is passed as a parameter during a function call, representing each element from the food list as item. Then, this statement, which you already have, can use that name, item, as the key to access the prices dictionary ...

total += prices[item]

#5

The instructions is strange :

"Write a function compute_bill that takes a parameter food as input"

the food list is not called food it's calles "shopping_list"!

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(shopping_list):
total = 0
for food in shopping_list:
total = total + prices[food] # this is the same as total += prices[food]
return total`


#6

The (food) in your function definition is just a place holder for whatever you pass to that function when you run it. so when you call the function with

compute_bill(shopping_list)

the argument (shopping_list) will replace (food) everywhere in the function. This is what allows you to pass any list to that function.

However the program wants me to come up with "10.5" as an answer, but the three items in the list only come out to "7.5" and my total keeps coming out to "4" for some reason?


#7

10.5 is what items in prices{} make altogether. Don't use shopping_list{}, but use prices{} instead.
Like this,
def compute_bill(food):
total=0
for item in food:
total+=prices[item]
return total


#8

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