Add basic Python calculator

This commit is contained in:
lev.karpov
2026-03-31 19:02:31 +00:00
parent fb62d97ab4
commit 42f36c2d31
2 changed files with 29 additions and 0 deletions

29
main.py Normal file
View File

@@ -0,0 +1,29 @@
def calculate(a, b, operation):
if operation == "+":
return a + b
if operation == "-":
return a - b
if operation == "*":
return a * b
if operation == "/":
if b == 0:
raise ValueError("Division by zero is not allowed")
return a / b
raise ValueError(f"Unsupported operation: {operation}")
def main():
print("Simple Python Calculator")
first = float(input("Enter the first number: "))
operation = input("Choose operation (+, -, *, /): ").strip()
second = float(input("Enter the second number: "))
try:
result = calculate(first, second, operation)
print(f"Result: {result}")
except ValueError as error:
print(f"Error: {error}")
if __name__ == "__main__":
main()