Python Example for Beginners| Mini Calculator

Step1:

print("Select operation.") 
print("1.Addition")
print("2.Subtraction")
print("3.Multiplication")
print("4.Division")

Step2:

while True: 
# Take input from the user
choice = input("Enter Your Choice(1 OR 2 OR 3 OR 4): ")
# Check if choice is one of the four options
if choice in ('1', '2', '3', '4'):
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
calculator(num1, num2, choice)
else:
print("Invalid Input")
break;

Step 3:

def calculator(x, y, choice): 
x = int(x)
y = int(y)
if choice == '1':
print(num1, "+", num2, "=", x + y)
elif choice == '2':
print(num1, "-", num2, "=", x - y)
elif choice == '3':
print(num1, "X", num2, "=", x * y)
elif choice == '4':
print(num1, "/", num2, "=", x / y)
$ python3 calculator.py 
Select operation.
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter Your Choice(1 OR 2 OR 3 OR 4): 1
Enter first number: 3
Enter second number: 5
3 + 5 = 8
Enter Your Choice(1 OR 2 OR 3 OR 4): 2
Enter first number: 5
Enter second number: 2
5 - 2 = 3
Enter Your Choice(1 OR 2 OR 3 OR 4): 5
Invalid Input
$

Summary

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CodingHub

CodingHub

Coding Hub — Learn & Build Your Career