Calculator program in python
In this python programming guide, we are going to learn
-
calculator program in python using if else
Calculator program in python using while loop
The calculator program in python using if else or calculator program in python using while loop is as follows:
# Owner : TutorialsInhand Author : Devjeet Roy
while True:
choice = input("1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Exit\n")
if choice == '1':
n1 = float(input("Enter first number: "))
n2 = float(input("Enter second number: "))
sum = n1 + n2
print("The sum is:",sum)
elif choice == '2':
n1 = float(input("Enter first number: "))
n2 = float(input("Enter second number: "))
diff = n1 - n2
print("The difference is:",diff)
elif choice == '3':
n1 = float(input("Enter first number: "))
n2 = float(input("Enter second number: "))
prod = n1 * n2
print("The product is:",prod)
elif choice == '4':
n1 = float(input("Enter first number: "))
n2 = float(input("Enter second number: "))
div = n1 / n2
print("The quotient is:",div)
elif choice == '5':
break
else:
print("Enter correct choice!!")
The output of calculator program in python using if else is as follows:
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
1
Enter first number: 12
Enter second number: 23
The sum is: 35.0
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
2
Enter first number: 123
Enter second number: 23
The difference is: 100.0
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
3
Enter first number: 12
Enter second number: 23
The product is: 276.0
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
4
Enter first number: 12
Enter second number: 2
The quotient is: 6.0
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
5
Few important tips about this program
1. We run an infinite loop and repeatedly as the user what he/she wants to do. Once they put their choice, they are asked for two numbers, which are type converted to float and the result of the operation is displayed.
2. The Exit options allows the programmer to break out of the loop and terminate the program.
3. For any other output, an error message is shown on the terminal.
Python calculator program:
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :
Published Date :
May 02,2021