Python programming function sucked in computing the bill at codecademy
Write a function compute_bill that takes a parameter food as input and
computes your bill by looping through the food list and summing the costs
of each item in the list.
For now, go ahead and ignore whether or not the item you're billing for is
in stock.
Note that your function should work for any food list.
The Given code is
groceries = ["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! I've written this
def compute_bill(food):
total = 0;
for f in food:
if stock[f] > 0:
total+=prices[f]
stock[f] -=1
return total
compute_bill(groceries)
Error message is
Oops, try again! You code does not seem to work when ['apple'] is used as
input--it returns 0 instead of 2.
No comments:
Post a Comment