in

How to Create Calculator in Python using If Elif Else

Create calculator in Python

In this post, You will learn how to create a calculator in Python using If Elif Else. You can use this calculator to perform basic arithmetic operations like addition, subtraction, division, and multiplication. Below is a simple calculator program in Python.

Code to Create a Calculator in Python

  • First, we create a function (addition, subtraction, division, and multiplication)
Calculator in Python
  • After that, we write a code to take arithmetic symbols from the user.
Calculator Program in Python using if elif else
  • Then we will take 2 numbers from the user.
  • Then we instruct our program to perform an arithmetic calculation based on user input.
  • We write else command in last. So if, the user enters invalid values.

Code:

def add(a,b):
print(a+b)
def sub(a,b):
print(a-b)
def multi(a,b):
print(a*b)
def div(a,b):
print(a/b)

print('''Select your operation:
Add(+)
Sub(-)
Multiply(*)
Divide(/)''')

select=input("Please Enter a Choice")

number1=input("Type First Number")
number2=input("type Second Number")
a=int(number1)
b=int(number2)

if select=="+":
add(a,b)
elif select=="-":
sub(a,b)
elif select=="*":
multi(a,b)
elif select=="/":
div(a,b)
else:
print("Please Enter a Valid Operation")
  1. The above code takes input from the user.
  2. The user will enter an arithmetic symbol and 2 digits to perform the calculation.
  3. After that code will print the result.

See More:

Remove YouTube Ads

Result:

Select your operation:
Add(+)
Sub(-)
Multiply(*)
Divide(/)
Please Enter a Choice+
Type First Number88
type Second Number55
143

Process finished with exit code 0

Lots of methods on the internet to create a calculator program in Python using If Elif Else. The above process is one of them.

Written by CoolzGeeks

Leave a Reply

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

Most InterestiNg Websites

Cool and Most Interesting Websites on the Internet