Making a Purchase (list/dictionary name problem)


#1



Here is the link to the exercice:

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


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!


#2

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.


#3

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