Abdnormal Abdou
Abdnormal Abdou

Reputation: 1

Python: what is wrong with this function?

this code is supposed to count the total of listed items price after it checks if the items are available on the stock (stock variable), I want to know why I doesn't print anything.

    shopping_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(food):
    total=0
    for i in food:
        if stock[i]>0:
            total+=prices[i]
    return total    
    print total
compute_bill(shopping_list)

Upvotes: 0

Views: 84

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

If running in an IDE you will need to use print

print compute_bill(shopping_list)

The print total is unreachable after the return statement.

So either forget about the print or put the print before the return

Upvotes: 0

crhodes
crhodes

Reputation: 1188

Your print needs to be before the return statement:

def compute_bill(food):
    total=0
    for i in food:
        if stock[i]>0:
            total+=prices[i]    
    print total
    return total

Upvotes: 1

Related Questions