Introduction to Python Modules — Question 21
Back to all questionsCreate a menu-driven program using user-defined functions to implement a calculator that performs the following:
(a) Basic arithmetic operations (+, −, *, /)
(b) log10(x), sin(x), cos(x)
import math
def arithmetic_operations():
print("Select operation:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
choice = input("Enter choice (1/2/3/4): ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print("Result:", num1 + num2)
elif choice == '2':
print("Result:", num1 - num2)
elif choice == '3':
print("Result:", num1 * num2)
elif choice == '4':
if num2 != 0:
print("Result:", num1 / num2)
else:
print("Error: Division by zero is not allowed.")
else:
print("Invalid input")
def mathematical_functions():
print("Select function:")
print("1. Logarithm base 10 (log10)")
print("2. Sine (sin)")
print("3. Cosine (cos)")
choice = input("Enter choice (1/2/3): ")
if choice == '1':
num = float(input("Enter a number for log10: "))
if num > 0:
print("Result:", math.log10(num))
else:
print("Error: Logarithm is defined for positive numbers only.")
elif choice == '2':
angle = float(input("Enter the angle in degrees: "))
radians = math.radians(angle)
print("Result:", math.sin(radians))
elif choice == '3':
angle = float(input("Enter the angle in degrees: "))
radians = math.radians(angle)
print("Result:", math.cos(radians))
else:
print("Invalid input")
while True:
print("\nCalculator Menu:")
print("1. Basic Arithmetic Operations")
print("2. Mathematical Functions")
print("3. Exit")
choice = input("Enter choice (1/2/3): ")
if choice == '1':
arithmetic_operations()
elif choice == '2':
mathematical_functions()
elif choice == '3':
print("Exiting the program.")
break
else:
print("Invalid input. Please choose again.")Calculator Menu:
1. Basic Arithmetic Operations
2. Mathematical Functions
3. Exit
Enter choice (1/2/3): 1
Select operation:
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
Enter choice (1/2/3/4): 2
Enter first number: 34
Enter second number: 7
Result: 27.0
Calculator Menu:
1. Basic Arithmetic Operations
2. Mathematical Functions
3. Exit
Enter choice (1/2/3): 2
Select function:
1. Logarithm base 10 (log10)
2. Sine (sin)
3. Cosine (cos)
Enter choice (1/2/3): 1
Enter a number for log10: 24
Result: 1.380211241711606
Calculator Menu:
1. Basic Arithmetic Operations
2. Mathematical Functions
3. Exit
Enter choice (1/2/3): 3
Exiting the program.
import
math
def
arithmetic_operations
():
print
(
"Select operation:"
)
print
(
"1. Addition (+)"
)
print
(
"2. Subtraction (-)"
)
print
(
"3. Multiplication (*)"
)
print
(
"4. Division (/)"
)
choice
=
input
(
"Enter choice (1/2/3/4): "
)
num1
=
float
(
input
(
"Enter first number: "
))
num2
=
float
(
input
(
"Enter second number: "
))
if
choice
==
'1'
:
print
(
"Result:"
,
num1
+
num2
)
elif
choice
==
'2'
:
print
(
"Result:"
,
num1
-
num2
)
elif
choice
==
'3'
:
print
(
"Result:"
,
num1
*
num2
)
elif
choice
==
'4'
:
if
num2
!=
0
:
print
(
"Result:"
,
num1
/
num2
)
else
:
print
(
"Error: Division by zero is not allowed."
)
else
:
print
(
"Invalid input"
)
def
mathematical_functions
():
print
(
"Select function:"
)
print
(
"1. Logarithm base 10 (log10)"
)
print
(
"2. Sine (sin)"
)
print
(
"3. Cosine (cos)"
)
choice
=
input
(
"Enter choice (1/2/3): "
)
if
choice
==
'1'
:
num
=
float
(
input
(
"Enter a number for log10: "
))
if
num
>
0
:
print
(
"Result:"
,
math
.
log10
(
num
))
else
:
print
(
"Error: Logarithm is defined for positive numbers only."
)
elif
choice
==
'2'
:
angle
=
float
(
input
(
"Enter the angle in degrees: "
))
radians
=
math
.
radians
(
angle
)
print
(
"Result:"
,
math
.
sin
(
radians
))
elif
choice
==
'3'
:
angle
=
float
(
input
(
"Enter the angle in degrees: "
))
radians
=
math
.
radians
(
angle
)
print
(
"Result:"
,
math
.
cos
(
radians
))
else
:
print
(
"Invalid input"
)
while
True
:
print
(
"
\n
Calculator Menu:"
)
print
(
"1. Basic Arithmetic Operations"
)
print
(
"2. Mathematical Functions"
)
print
(
"3. Exit"
)
choice
=
input
(
"Enter choice (1/2/3): "
)
if
choice
==
'1'
:
arithmetic_operations
()
elif
choice
==
'2'
:
mathematical_functions
()
elif
choice
==
'3'
:
print
(
"Exiting the program."
)
break
else
:
print
(
"Invalid input. Please choose again."
)
Output
Calculator Menu:
1. Basic Arithmetic Operations
2. Mathematical Functions
3. Exit
Enter choice (1/2/3): 1
Select operation:
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
Enter choice (1/2/3/4): 2
Enter first number: 34
Enter second number: 7
Result: 27.0
Calculator Menu:
1. Basic Arithmetic Operations
2. Mathematical Functions
3. Exit
Enter choice (1/2/3): 2
Select function:
1. Logarithm base 10 (log10)
2. Sine (sin)
3. Cosine (cos)
Enter choice (1/2/3): 1
Enter a number for log10: 24
Result: 1.380211241711606
Calculator Menu:
1. Basic Arithmetic Operations
2. Mathematical Functions
3. Exit
Enter choice (1/2/3): 3
Exiting the program.