user
Create a Python script that simulates a basic command-line calculator supporting addition, subtraction, multiplication, and division operations. PLAINFORMAT
assistant
import sys

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):
    if y == 0:
        raise ValueError("Cannot divide by zero")
    return x / y

def main():
    while True:
        try:
            x = float(input("Enter first number: "))
            y = float(input("Enter second number: "))
            operation = input("Choose an operation (+, -, *, /): ")

            if operation == "+":
                print(f"{x} + {y} = {add(x, y)}")
            elif operation == "-":
                print(f"{x} - {y} = {subtract(x, y)}")
            elif operation == "*":
                print(f"{x} * {y} = {multiply(x, y)}")
            elif operation == "/":
                print(f"{x} / {y} = {divide(x, y)}")
            else:
                print("Invalid operation. Please choose from +, -, *, /")

            continue_calculation = input("Do you want to continue? (yes/no): ")
            if continue_calculation.lower() != "yes":
                break
        except ValueError as e:
            print(e)
            print("Please enter valid numbers.")
        except Exception as e:
            print(e)
            print("An error occurred. Exiting the calculator.")
            sys.exit(1)

if __name__ == "__main__":
    main()