human
How can I create a calculator in both JavaScript and Python?
gpt
Creating a calculator in JavaScript and Python involves defining the basic operations (addition, subtraction, multiplication, and division), setting up the user interface (for JavaScript), and taking user input (for Python).

Here's a simple implementation for both languages:

1. **JavaScript (HTML + JavaScript):**
```html
<!DOCTYPE html>
<html>
    <head>
        <title>Simple Calculator</title>
        <style>
        input[type=button] {
            width: 60px;
            height: 60px;
        }
        </style>
        <script>
        function add(x, y) {
            return x + y;
        }

        function subtract(x, y) {
            return x - y;
        }

        function multiply(x, y) {
            return x * y;
        }

        function divide(x, y) {
            return x / y;
        }

        function input(value) {
            document.getElementById('display').value += value;
        }

        function calculate() {
            const expression = document.getElementById('display').value;
            const result = eval(expression);
            document.getElementById('display').value = result;
        }

        function clearDisplay() {
            document.getElementById('display').value = '';
        }
        </script>
    </head>
    <body>
        <h1>Simple Calculator</h1>
        <input type="text" id="display" readonly>
        <br>
        <input type="button" value="1" onclick="input('1')">
        <input type="button" value="2" onclick="input('2')">
        <input type="button" value="3" onclick="input('3')">
        <input type="button" value="+" onclick="input('+')">
        <br>
        <input type="button" value="4" onclick="input('4')">
        <input type="button" value="5" onclick="input('5')">
        <input type="button" value="6" onclick="input('6')">
        <input type="button" value="-" onclick="input('-')">
        <br>
        <input type="button" value="7" onclick="input('7')">
        <input type="button" value="8" onclick="input('8')">
        <input type="button" value="9" onclick="input('9')">
        <input type="button" value="*" onclick="input('*')">
        <br>
        <input type="button" value="0" onclick="input('0')">
        <input type="button" value="C" onclick="clearDisplay()">
        <input type="button" value="=" onclick="calculate()">
        <input type="button" value="/" onclick="input('/')">
    </body>
</html>
```
2. **Python:**
```python
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

def calculator():
    print("Select operation:")
    print("1. Add")
    print("2. Subtract")
    print("3. Multiply")
    print("4. Divide")

    choice = input("Enter choice (1, 2, 3, 4): ")

    num1 = float(input("Enter first number: "))
    num2 = float(input("Enter second number: "))

    if choice == '1':
        print("Result:", add(num1, num2))
    elif choice == '2':
        print("Result:", subtract(num1, num2))
    elif choice == '3':
        print("Result:", multiply(num1, num2))
    elif choice == '4':
        print("Result:", divide(num1, num2))
    else:
    print("Invalid input")

    if name == "main":
    while True:
    calculator()
    keep_going = input("Do you want to continue? (yes or no): ")
    if keep_going.lower() != "yes":
    break
```

For the JavaScript calculator, save the HTML + JavaScript code in a file with an `.html` extension and open it in a web browser. The calculator will appear, and you can perform calculations using the buttons.

For the Python calculator, save the Python code in a file with a `.py` extension and run it using a Python interpreter (Python 3.x). The calculator will prompt you to choose an operation, input numbers, and display the result. To exit the calculator, enter "no" when prompted to continue.