Stocking Out


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096#

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):
    total=0
    
    for item in food:
        while stock[item]>0:
            total += prices[item]
            stock[item] -= 1
    return total


#2

there is a difference between if and while.

for example

count=10

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]


#3

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