0

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)

2 Answers 2

1

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
answered Sep 25, 2014 at 23:24
Sign up to request clarification or add additional context in comments.

1 Comment

another question, shouldn't it prints (4+1.5+3 )= 8.5 instead of 5.5?
0

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

answered Sep 25, 2014 at 23:24

5 Comments

another question, shouldn't it prints (4+1.5+3 )= 8.5 instead of 5.5?
No, there is no apples in stock so if stock[i]>0: will be False for apples and the price won't be added so 5.5 is correct
I know that there is no apple in the stock that's why I said it should be 8.5, if the apple was in stock it would've been 10.5 !
why didn't you add "pear"? it is available in the stock!
where do you see pear in the shopping list?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.