43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
|
|
#!/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()
|