Problem in Making a Purchase


What's wrong in the below code f mine?

Error shown is: "Oops, try again. compute_bill(['apple']) returned None, did you remember to return the result?" Isn't none the right answer as there is no stock of apples.

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):
    for item in food:
        if stock[item]!=0:
    print total

print compute_bill(shopping_list)


The error tells you to return. Change this way:
return total


Thank you so much.
I editted the code but now I'm getting this error: "Oops, try again. compute_bill(['apple']) returned 0 instead of 2". The stock of apples is 0. Shouldn't it return 0?

Edit: Nevermind, I just "increased" the stock of apple by one and it worked.


Well, your loop control

should be causing that. Remove it to have this.

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


Thanks I realised it didn't need that condition.



I was wondering why you had to increase the apple stock. That would have been "cheating". :slight_smile:

Yoou're welcome. @cssplayer63783