Python Program to Make a Simple Calculator – 2024

Hey guys, in this blog we will see a Python Program to Make a Simple Calculator.

Code

# Python Program to Make a Simple Calculator

while True:
    # take input from the user
    choice = input("1.Add \n2.Subtract \n3.Multiply \n4.Divide \n0.Exit \nEnter choice: ")

    # check if choice is one of the four options
    if choice in ('1', '2', '3', '4'):
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))

        if choice == '1':
            print(num1, "+", num2, "=", num1+num2)

        elif choice == '2':
            print(num1, "-", num2, "=", num1-num2)

        elif choice == '3':
            print(num1, "*", num2, "=", num1*num2)

        elif choice == '4':
            print(num1, "/", num2, "=", num1/num2)
            
        elif choice == '4':
            break
            
    else:
        print("Invalid Input")

Output

1.Add 
2.Subtract 
3.Multiply 
4.Divide 
0.Exit 
Enter choice: 1
Enter first number: 2
Enter second number: 3
2.0 + 3.0 = 5.0

1.Add 
2.Subtract 
3.Multiply 
4.Divide 
0.Exit 
Enter choice: 4
Enter first number: 8
Enter second number: 3
8.0 / 3.0 = 2.6666666666666665

1.Add 
2.Subtract 
3.Multiply 
4.Divide 
0.Exit 
Enter choice: 0

Simply taking two numbers to perform actions on.

  • If the user inputs 1, it will perform add operation.
  • If the user inputs 2, it will perform subtract operation.
  • If the user inputs 3, it will perform a multiply operation.
  • If the user inputs 4, it will perform a divide operation.
  • If the user inputs 0, it will close the calculator.

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 517

Leave a Reply

Your email address will not be published. Required fields are marked *