Stocking Out


If i use a while loop instead of an if statement the following error message is observed:
Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 69 instead of the correct 7

I expected it to behave in the same manner an if statement would but seems i cant figure out the difference between the two. Can someone help??

 def compute_bill(food):
    for item in food:
        while stock[item]>0:
            total += prices[item]
            stock[item] -= 1
    return total


there is a difference between if and while.

for example


if count>0:
  print count


while count>0:
  print count
  count-= 1

If you run both loop you'll see if block prints one time.but while block prints as long as the condition will be True.
Note the the last line of code if you don't do this it'll be infinite loop.

In your code add a print statement after while loop

print stock[item]


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