Creating a calculator using if/else statements in python
We can build the calculator using the if-else statement in python. but,me can performance any of the mathematical operation in it like(+,-,×,÷,%). The output of the program will be performs the mathematical operation based on the user selection only.the user can select any one of the mathematical operator at a time.
Program
n1=float(input('enter first number:'))
n2=float(input('enter second number:'))
Op=input(input('enter the operation(+,-,×,÷,%):'))
Prnit(n1,op,n2)
Result=calculate(n1,op,n2)
Print('=',Result)
def calculate(n1,n2,op):
if op == '+':
result = n1+n2
elif op == '-':
result = n1-n2
elif op == '*':
result = n1*n2
elif op == '/':
result = n1/n2
elif op=='%':
result = n1%n2
return result
Output
enter first number: enter second number: enter the operation(+,-,×,÷,%):
Note:-
The user want to the select within the 5 operator’s only. otherwise, the program doesn’t shows the output.