Making a Purchashe


#1



Please guys help me solve this. I tried every i can think of and i always get errors.


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


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


#2

here:

item = prices[key]

key is undefined, you want to get the price of item (which contains items from food list)


#3

I did this:
def compute_bill(food):
total = 0
for item in food:
total += prices[item]
return total
but still not working.


#4

better, nearly there

a function ends the moment a return keyword is reached

so your function ends in the first iteration/run of your loop, to get to the correct total the whole loop needs to run


#5

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