journal
Week 4unknown
python
a year ago
2.2 kB
5
Indexable
def addition(a, b): """Perform addition of two numbers.""" 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 def display_menu(): """Display the menu of options to the user.""" print("\nSimple Calculator Menu") print("1. Addition") 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: display_menu() choice = get_user_choice() if choice == 5: print("Exiting the program.") break num1, num2 = get_numbers() if choice == 1: result = addition(num1, num2) elif choice == 2: result = subtraction(num1, num2) elif choice == 3: result = multiplication(num1, num2) elif choice == 4: result = division(num1, num2) print(f"The result is: {result}") if __name__ == "__main__": main()
Editor is loading...
Leave a Comment