Add simple calculator program
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
43
exercise.py
Normal file
43
exercise.py
Normal file
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Simple calculator program for two numbers.
|
||||
Supports addition, subtraction, multiplication, and division.
|
||||
"""
|
||||
|
||||
def calculator():
|
||||
"""Main calculator function."""
|
||||
print("Simple Calculator")
|
||||
print("Operations: + (addition), - (subtraction), * (multiplication), / (division)")
|
||||
|
||||
try:
|
||||
# Get input from user
|
||||
num1 = float(input("Enter first number: "))
|
||||
num2 = float(input("Enter second number: "))
|
||||
operation = input("Enter operation (+, -, *, /): ").strip()
|
||||
|
||||
# Perform calculation
|
||||
if operation == '+':
|
||||
result = num1 + num2
|
||||
print(f"{num1} + {num2} = {result}")
|
||||
elif operation == '-':
|
||||
result = num1 - num2
|
||||
print(f"{num1} - {num2} = {result}")
|
||||
elif operation == '*':
|
||||
result = num1 * num2
|
||||
print(f"{num1} * {num2} = {result}")
|
||||
elif operation == '/':
|
||||
if num2 == 0:
|
||||
print("Error: Division by zero!")
|
||||
else:
|
||||
result = num1 / num2
|
||||
print(f"{num1} / {num2} = {result}")
|
||||
else:
|
||||
print("Invalid operation! Please use +, -, *, or /.")
|
||||
|
||||
except ValueError:
|
||||
print("Invalid input! Please enter numeric values.")
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
calculator()
|
||||
Reference in New Issue
Block a user