Untitled

mail@pastecode.io avatar
unknown
plain_text
5 days ago
1.5 kB
0
Indexable
Never
# Function to add two numbers
def add(x, y):
    return x + y

# Function to subtract two numbers
def subtract(x, y):
    return x - y

# Function to multiply two numbers
def multiply(x, y):
    return x * y

# Function to divide two numbers
def divide(x, y):
    if y == 0:
        return "Error: Division by zero!"
    else:
        return x / y

# Main program loop
while True:
    print("Options:")
    print("Enter 'add' for addition")
    print("Enter 'subtract' for subtraction")
    print("Enter 'multiply' for multiplication")
    print("Enter 'divide' for division")
    print("Enter 'quit' to end the program")
    user_input = input(": ")

    if user_input == "quit":
        break
    
    if user_input not in ["add", "subtract", "multiply", "divide"]:
        print("Unknown input. Please enter a valid option.")
        continue
    
    # Get input numbers
    try:
        num1 = float(input("Enter first number: "))
        num2 = float(input("Enter second number: "))
    except ValueError:
        print("Invalid input. Please enter numeric values.")
        continue
    
    # Perform calculation based on user input
    if user_input == "add":
        print("Result:", add(num1, num2))
    elif user_input == "subtract":
        print("Result:", subtract(num1, num2))
    elif user_input == "multiply":
        print("Result:", multiply(num1, num2))
    elif user_input == "divide":
        print("Result:", divide(num1, num2))
Leave a Comment