Apoorv Aditya
Apoorv Aditya

Reputation: 3

Candy dispenser in python

when i run this code, total available candies is initialised as 10 in the first line, still it terminates the code after dispensing 9 candies. plz help

av = 10
while True:
    x = int(input("How many candies do you need? "))
    if x <= 10:
        av = av - x
        if av>0:
            for i in range(0,x):
                print("Candy")
        else:
            print("We are out of stock!!")
            break
    else:
        print("Unable to dispense candies")

Upvotes: 0

Views: 250

Answers (1)

Alexandru Sandu
Alexandru Sandu

Reputation: 143

You need to change the condition to
if av>=0: in your code, as your not taking into account the case when the last transaction actually empties the stack of candies

Upvotes: 5

Related Questions