journal

Week 4
unknown
python
10 days ago
2.2 kB
1
Indexable
Never
```def addition(a, b):
return a + b

def subtraction(a, b):
"""Perform subtraction of two numbers."""
return a - b

def multiplication(a, b):
"""Perform multiplication of two numbers."""
return a * b

def division(a, b):
"""Perform division of two numbers."""
if b == 0:
return "Error: Division by zero is not allowed."
return a / b

"""Display the menu of options to the user."""
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Exit")

def get_user_choice():
"""Get the user's choice from the menu."""
while True:
try:
choice = int(input("Enter your choice (1-5): "))
if 1 <= choice <= 5:
return choice
else:
print("Invalid choice. Please enter a number between 1 and 5.")
except ValueError:
print("Invalid input. Please enter a number between 1 and 5.")

def get_numbers():
"""Get two numbers from the user for arithmetic operations."""
while True:
try:
num1 = int(input("Enter the first number (integer): "))
num2 = float(input("Enter the second number (float): "))
return num1, num2
except ValueError:
print("Invalid input. Please enter a valid integer and float.")

def main():
"""Main function to run the calculator program."""
while True:
choice = get_user_choice()

if choice == 5:
print("Exiting the program.")
break

num1, num2 = get_numbers()

if choice == 1: