Making a purchase


shopping_list = ["banana", "apple", "orange", "pear"]

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 prices:
        total += prices[item]
        return total

Oops, try again. compute_bill(['apple']) returned 1.5 instead of 2

Making a Purchase

that is my code up above, along with the error message. seems to be running , but getting orange and apple mixed up? what am i doing wrong? thanks


no, you loop over prices rather then food (the function parameter which contains your shopping list.

a function ends the moment a return keyword is reached, this is currently happening in the first iteration of your loop, you might want to change the indent of return so the whole loop can run.

also, in the function call, shopping_list is a variable (which you defined on line 1), not a string


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