Making a purchase


#1

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

My code returned 7.5 instead of 2 for some reason.

hopping_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(item):
    total=0
    for item in shopping_list:
        total+= prices[item]
    return total
print compute_bill(prices)


#2

why are you expecting 2 as the correct answer?


#3

Codecademy's submission correctness test (SCT) may have issued the following message ...

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

Hi @cloudpro63665,

In your compute_bill function, iterate through the list that is specified as the parameter in the function header.

You have this as the function header ...

def compute_bill(item):

A better name for the function parameter would be food, which you can specify as follows ...

def compute_bill(food):

Then, to loop through food, which will refer to a list when the function is called, you can have something like this as the for loop header ...

    for item in food:

With that for loop header, item will represent each item within the list, food, as the loop iterates.

EDITED on March 11, 2017 to provide additional information regarding iterating through a list.


#4

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