Supermarket checkout gives error but code appears correct


#1



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


Oops, try again. stock doesn't look quite right! Make sure to not call compute_bill since it changes the stock! It should contain: {'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0}


This is the printout and I assume this is supposed to be what happens.
banana
instock 6
price 4
total 4
pear
instock 15
price 3
total 7
(7, {'orange': 32, 'pear': 14, 'banana': 5, 'apple': 0})
None

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(food):
    total = 0
    for item in food:
        print item
        price = prices[item]
        instock = stock[item]
        print "instock",instock
        if instock > 0:
            print "price",price
            total = total + price
            print "total",total
            stock[item] -= 1
    return total, stock

stuff = ["banana","pear"]

print compute_bill(stuff)

Thanks for any help
Peter


#3

There is way more code than required for this exercise. We are only computing, not printing. Keep your code as simple as you can. The dictionaries themselves are the only variables you need, apart from food, item and total.

    total = 0
    for item in food:
        if stock[item] > 0:
            total += prices[item]
            stock[item] -= 1
    return total

Focus on what is expected and you'll find these lessons easier to pass. Once you have passed the lesson, you are free to tinker and experiment.


#4

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