Not really getting this compute_bill thing


Oops, try again. compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5

For it to work... I read other discussions and get how functions, and dictionaries work more than I did before. But still run into a problem :confused:

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


well, you seem to have get most of it right :slight_smile:

A function ends the moment a return keyword is reached, if a return keyword is reached in a loop, the loop simply breaks. (this is currently happening to your code)

Place return outside the for loop so your whole loop can run


Thank you, I didn't know that! And do functions only call lists in the def(argument) parenthesis? If so what other things can they call? The argument is the one thing that confuses me about functions.


okay, so here you declare the function:

def compute_bill(food):

food is the function parameter. You can pass anything as argument when calling the function:

print compute_bill(shopping_list)

so shopping_list is now your argument. You can pass anything as argument, but your function needs to be capable of handling it.


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