11. Making a Purchase



compute_bill(['apple']) resulted in a NameError: global name 'total' 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

def compute_bill(food):
    for food in stock:
    return total


Did you mean to write, total = total + prices[food]?

In any case, it would still be incorrect code but it does address the errant operator. Consider that food is the argument that is meant to be used as the list object in our loop. So we would write,

for item in food:

Give these two points some thought and the solution will unfold.


Yeah I ended up figuring it out. Correcting the code and all. Thanks though!


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