Coding logic


#1

Regarding this problem, could anyone teach me how to take out “apple” from the original code:

stock = {
“banana”: 6,
“apple”: 0,
“orange”: 32,
“pear”: 15
}

prices = {
“banana”: 4,
“apple”: 2,
“orange”: 1.5,
“pear”: 3

#start coding here
def compute_bill(food):
total = 0
for item in food:
total = total + prices[item] * stock[item]
return total

This is what I did to take out apple from the stock since it only has zero, and I don’t know why it does not work, could anyone kindly explain? Thanks

total = total + prices[item] * (stock[item]-stock[“apple”])


#2

That is good logic, just not expected by the SCT, perhaps?

    if stock[item] > 0:

Had a chance to rethink your logic, yet? What if there are more than zero items in stock? The bill would be for that many, rather than one of each available.


#3

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