Please help


#1



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


#2

hello @sygon,
Can you mention what exercise you exactly at and also what error you're getting?
That'd be helpful for us debug the code.

The problem that's visible now.

item is an iterator and defined on the fly while looping over food.
so you can't use it before you define it,
The if part should be inside the loop.


#3

This particular activity is somewhat misleading. I am not particularly a fan of how codeacademy tests your input behind the scenes, they should allow you to call the function yourself, and pass whatever parameters it requires in manually if you want.

For example, in this particular lesson, they do not explain at all that the food parameter for compute_bill, is actually the shopping list array. Check my solution below, this is all you need to do:

def compute_bill(food):
    total = 0
    for item in food:
        total += prices[item]
    return total

#4

Hello @dzuz14,
If you check different web-sites, they do almost the same , They check the code on the different inputs behind the scenes

Also check this thread.


#5

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