Making a Purchase error


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096


ERROR: Oops, try again. compute_bill(['apple']) returned 10.5 instead of 2


I'm trying to figure out what I am doing wrong and feel stuck


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

print compute_bill(shopping_list)


#2

You are using your own shopping list! The thing is supposed to run for all food lists, and they have multiple lists they run to test.

You are also looking at the elements in the prices list instead of adding things in the food list. Using prices[item] will call on the price of the item.

Instead, maybe try:

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

#3

tried this error says global variable prices not defined


#4

what is the rest of your code?


#5

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