11. Making a Purchase


#1



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


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):
    total==0
    for food in stock:
        total==total+prices[food]
    return total


#2

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.


#3

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


#4

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