# What is the error in this code

#1

shopping_list = [ "banana","apple","pear"]

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:
temp=stock[i]
if temp > 0:
while temp > 0:
if i=="banana":
total+=prices["banana"]

if i=="apple":
total+=prices["apple"]

if i=="orange":
total+=prices["orange"]

if i=="pear":
total+=prices["pear"]

temp=temp-1

print compute_bill(shopping_list)

#2

The number one reason for using a dynamic variable is so we do not have to repeat code patterns, just swap in a new value.

``for item in food:``

`item` is the dynamic variable that will take on each item in the list, one at a time (in any order).

``````    if stock[item] > 0:
total += prices[item]
stock[item] -= 1``````

Much simpler and with no repetition.

#3

But this will change the stock which are given to us

#4

Not sure what lesson this is; please post a link to the exercise. Thanks.

#5

#6

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