Making a Purchase (list/dictionary name problem)


Here is the link to the exercice:

The code doesn't run if i dont use the word "prices" as a dictionnary input for my function, and I am wondering why. If I try to make it work for just a nonspecific "food" dictionnary, it stops working:

Oops, try again. compute_bill(['apple']) resulted in a TypeError: list indices must be integers, not str

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

Thanks for your help!


prices is our global variable that have a data-base for our fruit price rates.
food here is a list of fruits, so if we do this line..

total += food[item]

and as food is a list and needed "integers" to access its element will give an error.

TypeError: list indices must be integers, not str

The design of our function is such that it takes list of items and uses global variable prices and finds total price of fruits someone buying.


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