Help please! A Day at the Supermarket - CONFUSED


#1

I've finished the exercise but don't understand how to use it. I understand that the code is designed to track the stock count and to generate the sum of selling all items in food - but how do I get that to print to the console?

My code is behaving fine. It would just help me to retain the information if I knew how to use it.

I expected the total to print out in the console as well as the stock count if I print them, but it didn't work. Nothing appears in the console when I run the code.

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):# Write your code below!
    total = 0
    for item in food:
        if stock[item] > 0:
            total += prices[item]
            stock[item] -= 1
    return total

    print total
    print stock[item]

Any help would be much appreciated!


#2

you are printing inside your function, you need to call your function to print or just print stock


#3

Thanks for your reply. Could you show me what the print statement should look like and where it should go?

Thanks a lot


#4

def compute_bill(food):
    total = 0
    for key in food:
        if stock[key] > 0:
            total += prices[key]
            stock[key] -= 1
    return total
print compute_bill(["banana", "orange"])

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}

it changes stock since food holds only two values

this is just example how to call your function in future :wink:


#5

@knezknele

I get this error:

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}

My code:

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

food=["banana", "pear", "apple","orange"]
print compute_bill(food)

Need help