hi
- User
hi
- Assistant
Hello! How can I assist you today?
- User
Write a python calculator
- Assistant
Sure! Here is a simple Python calculator that can perform addition, subtraction, multiplication, and division:
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: return "Error: Cannot divide by zero" return x / y 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")You can copy and paste this code into a Python environment and run it to perform calculations. Let me know if you need any further assistance!
- User
add the loop
- Assistant
Certainly! Here is the updated Python calculator with a loop that allows the user to perform multiple calculations without exiting the program:
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: return "Error: Cannot divide by zero" return x / y while True: print("Select operation:") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") print("5. Exit") choice = input("Enter choice (1/2/3/4/5): ") if choice == '5': print("Exiting calculator...") break 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")Now, the calculator will continue to run in a loop until the user chooses to exit by entering '5'. This allows the user to perform multiple calculations without restarting the program. Feel free to run this code and let me know if you have any questions!
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
- gpt-3.5-turbo-0301Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →
- gpt-4-0314Compare side by side →