Making a Purchase error


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)


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


tried this error says global variable prices not defined


what is the rest of your code?


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