Write a menu-driven program to implement a simple calculator for two numbers given by the user.
while True:
print("Simple Calculator")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Exit")
choice = input("Enter choice (1/2/3/4/5): ")
if choice in ['1', '2', '3', '4', '5']:
if choice == '5':
print("Exiting the calculator.")
break
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
result = num1 + num2
print(num1, "+", num2, "=", result)
elif choice == '2':
result = num1 - num2
print(num1, "-", num2, "=", result)
elif choice == '3':
result = num1 * num2
print(num1, "*", num2, "=", result)
elif choice == '4':
if num2 != 0:
result = num1 / num2
print(num1, "/", num2, "=", result)
else:
print("Error! Division by zero.")
else:
print("Invalid choice! Please select a valid option.")Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 1
Enter first number: 234
Enter second number: 33
234.0 + 33.0 = 267.0
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 2
Enter first number: 34
Enter second number: 9
34.0 - 9.0 = 25.0
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 3
Enter first number: 2
Enter second number: 45
2.0 * 45.0 = 90.0
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 4
Enter first number: 452
Enter second number: 4
452.0 / 4.0 = 113.0
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 5
Exiting the calculator.
while
True
:
print
(
"Simple Calculator"
)
print
(
"1. Add"
)
print
(
"2. Subtract"
)
print
(
"3. Multiply"
)
print
(
"4. Divide"
)
print
(
"5. Exit"
)
choice
=
input
(
"Enter choice (1/2/3/4/5): "
)
if
choice
in
[
'1'
,
'2'
,
'3'
,
'4'
,
'5'
]:
if
choice
==
'5'
:
print
(
"Exiting the calculator."
)
break
num1
=
float
(
input
(
"Enter first number: "
))
num2
=
float
(
input
(
"Enter second number: "
))
if
choice
==
'1'
:
result
=
num1
+
num2
print
(
num1
,
"+"
,
num2
,
"="
,
result
)
elif
choice
==
'2'
:
result
=
num1
-
num2
print
(
num1
,
"-"
,
num2
,
"="
,
result
)
elif
choice
==
'3'
:
result
=
num1
*
num2
print
(
num1
,
"*"
,
num2
,
"="
,
result
)
elif
choice
==
'4'
:
if
num2
!=
0
:
result
=
num1
/
num2
print
(
num1
,
"/"
,
num2
,
"="
,
result
)
else
:
print
(
"Error! Division by zero."
)
else
:
print
(
"Invalid choice! Please select a valid option."
)
Output
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 1
Enter first number: 234
Enter second number: 33
234.0 + 33.0 = 267.0
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 2
Enter first number: 34
Enter second number: 9
34.0 - 9.0 = 25.0
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 3
Enter first number: 2
Enter second number: 45
2.0 * 45.0 = 90.0
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 4
Enter first number: 452
Enter second number: 4
452.0 / 4.0 = 113.0
Simple Calculator
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter choice (1/2/3/4/5): 5
Exiting the calculator.