A Day at the Super Market


HELP PLEASE! I'm stuck and i don't know what to do

11.Making Purchase

It shows that Oops, try again. compute_bill(['apple']) returned 10.5 instead of 2

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


your function:

def compute_bill(food):

the function parameter food will hold a list of items you want to buy, so you might want to re-think your loop


should i replace food with shopping_list


not really, look:

def compute_bill(food):
    print food

# function call, remove to pass the exercise

see how the argument (shopping_list) we supply when calling the function is copied into the function parameter, so you should loop over food, this way, your function works for many lists you supply as argument

After are done, remove the function call, otherwise you can't pass the exercise


experts in Python, please help me out.I am very new to python programming.Fortunately, data camp and codecademy helped me to reach this stage.I have been struggling with the following code.

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

Error msg: calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 4 instead of the correct 7


a function ends the moment a return keyword. maybe you want to change some indention so return is reached after the for loop rather then in the first iteration


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