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)
- After that, we write a code to take arithmetic symbols from the user.
- 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")
- The above code takes input from the user.
- The user will enter an arithmetic symbol and 2 digits to perform the calculation.
- After that code will print the result.
See More:
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.