write me a calculator
- User
write me a calculator
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
write me a calculator
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll create a simple command-line calculator in Python that handles basic arithmetic operations:
def calculator():
"""
A simple command-line calculator that supports basic arithmetic operations.
"""
print("Simple Calculator")
print("================")
print("Operations: +, -, *, /, ** (power), // (floor division), % (modulo)")
print("Type 'quit' or 'exit' to stop")
print("Type 'clear' to reset")
print()
result = None
while True:
try:
# Get input
if result is None:
user_input = input("Enter calculation: ")
else:
print(f"Current result: {result}")
user_input = input("Enter operation and number (or new calculation): ")
# Check for exit commands
if user_input.lower() in ['quit', 'exit']:
print("Thanks for using the calculator!")
break
# Check for clear command
if user_input.lower() == 'clear':
result = None
print("Calculator cleared.\n")
continue
# If there's a stored result and input starts with an operator
if result is not None and user_input.strip()[0] in ['+', '-', '*', '/', '%']:
# Continue calculation with stored result
expression = str(result) + user_input
else:
# New calculation
expression = user_input
result = None
# Evaluate the expression
calculated_result = eval(expression)
result = calculated_result
print(f"Result: {result}")
print()
except ZeroDivisionError:
print("Error: Division by zero!")
print()
except (ValueError, SyntaxError, NameError):
print("Error: Invalid input. Please enter a valid mathematical expression.")
print()
except Exception as e:
print(f"Error: {e}")
print()
if __name__ == "__main__":
calculator()
This calculator includes:
To use it:
5 + 3 or 10 * 2.5* 2 (multiplies previous result by 2)Would you like me to create a different type of calculator (GUI, web-based, scientific) or add more features to this one?