Stocking Out


#1



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


it says that 'total' isn't defined. NameError: name 'total' is not defined


i expected it to work and subtract one from the stock and add it to my total


shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!
def compute_bill(shopping_list):
    total = 0
    for key in shopping_list:
        if stock[key] > 0:  
            total = total + prices[key]
            stock[key] = stock[key] - 1
    return total
    
print total


#2

the problem is in your final line:

print total

total only exist inside the function (it has a local scope) so trying to access total outside the function throws an error


#3

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