How can I create a calculator in both JavaScript and Python?
- User
How can I create a calculator in both JavaScript and Python?
- Assistant
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:
- JavaScript (HTML + JavaScript):
<!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>- 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": breakFor the JavaScript calculator, save the HTML + JavaScript code in a file with an
.htmlextension 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
.pyextension 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.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →