Sunday, 5 January 2025

Calculator

def calculator():
    print("Welcome to the Basic Calculator!")
    print("Options: add, subtract, multiply, divide")
    
    # Get user inputs
    operation = input("Enter the operation: ").strip().lower()
    a = float(input("Enter the first number: "))
    b = float(input("Enter the second number: "))
    
    # Perform the calculation
    if operation == "add":
        result = a + b
    elif operation == "subtract":
        result = a - b
    elif operation == "multiply":
        result = a * b
    elif operation == "divide":
        if b == 0:
            return "Error: Division by zero"
        result = a / b
    else:
        return "Invalid operation"
    
    return f"The result is: {result}"

# Run the calculator
print(calculator())

No comments:

Post a Comment