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
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
Sign up to request clarification or add additional context in comments.
1 Comment
Abdnormal Abdou
another question, shouldn't it prints (4+1.5+3 )= 8.5 instead of 5.5?
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
Padraic Cunningham
181k30 gold badges264 silver badges327 bronze badges
5 Comments
Abdnormal Abdou
another question, shouldn't it prints (4+1.5+3 )= 8.5 instead of 5.5?
Padraic Cunningham
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 correctAbdnormal Abdou
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 !
Abdnormal Abdou
why didn't you add "pear"? it is available in the stock!
Padraic Cunningham
where do you see
pear in the shopping list?lang-py