I am not able to understand the logic behind the piece of code


I have a doubt . Without passing values to the argument food how does it process the item in food and returns the total.Please help me understanding the logic behind this. The code works but I don't understand the logic behind it

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

Replace this line with your code.


function don't execute unless you call them, so when you call the function you should pass an argument (shopping_list for example):

print compute_bill(shopping_list)

to satisfy the function parameter food.


but now, since I haven't passed any values to the argument. Which argument does it take for food to get the desired result for total


the function won't be executed, and until that time food can serve as placeholder.


Hi, @bvicky.2205 ,

If nobody calls the compute_bill function, it does not execute at all, and so does not compute any value for total.

However, after you submit your code, Codecademy calls your compute_bill function to test it, and when doing so, it passes it a list of food items. Since Codecademy knows what was in its list, it can compare the value that your function returns during that test to what it should return. If those two values are not the same, you'll get an "Oops message, such as ...

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

Notice that inside that message is this ...


That is because Codecademy created its own list that contained only 'apple', in order to test your (actually, my deliberately buggy) function, and the function failed the test.


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