Problem in Making a Purchase


#1



What's wrong in the below code f mine?


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


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

print compute_bill(shopping_list)


#2

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


#3

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.


#4

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

#5

Thanks I realised it didn't need that condition.


#6

Yup!

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

Yoou're welcome. @cssplayer63783