Untitled
unknown
python
3 years ago
828 B
6
Indexable
# Define a function for each basic math operation def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): return x / y # Prompt the user for input num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) # Prompt the user to choose an operation print("Select operation:") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") # Use a dictionary to store the functions for each operation operation = int(input("Enter operation number: ")) operations = { 1: add, 2: subtract, 3: multiply, 4: divide, } # Use the chosen operation to perform the calculation result = operations[operation](num1, num2) # Print the result to the user print(f"Result: {result}")
Editor is loading...