Making a purchase


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


why are you expecting 2 as the correct answer?


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.


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