# A simple Candy Machine in Python

Hey Pythoneers,

You might have seen people using a booth like a machine for purchasing candies or soda. Well, have you ever wondered how it works?

In this tutorial, I’ll show you the basic logic used by a candy machine.

## Simple candy or soda machine in Python

The following code shows a very simple logic that can be used by a beginner to build a candy machine.

```print("     WELCOME    ")
x=int(input("How many candies do you want?"))
stock=50
i=1
while i<=x:
if i>stock:
print("OUT OF STOCK")
break
print("CANDY")
i+=1
print("THANKYOU")```

### Output:

Case 1: When the no. of candies is less than the total stock

```        WELCOME
How many candies do you want?
5
CANDY
CANDY
CANDY
CANDY
CANDY

```

Case 2: When the no.of candies is more than the total stock

```                 WELCOME
How many candies do you want?
100
OUT OF STOCK

```

Suppose, your candy machine has a stock of 50 candies. When you input 5 candies, the machine will first check if the input number is less than the total stock.
If yes, it will proceed and roll out your 5 candies. Here, I have just printed ‘CANDY’ 5 times. Practically, it should be connected to hardware to roll out ‘n’ number of candies.
But if you enter suppose, 55 which is more than the stock, the machine should display a relevant message to the customer.

`OUT OF STOCK`

I hope you have established a basic idea of how candy machines are built. All big softwares are created through basic logic. So, keep trying something new and create new things with all your knowledge.