TermPY/packages/calc.py

31 lines
831 B
Python
Raw Permalink Normal View History

2023-09-21 23:49:53 +00:00
# ["calc", "packages.calc", ["calc"]]
# Made By YourNameHere
2023-09-21 23:50:17 +00:00
def calc(command: list):
2023-09-21 23:49:53 +00:00
if len(command) != 3:
print("Usage: calculator [operand1] [operator] [operand2]")
return
operand1 = float(command[0])
operator = command[1]
operand2 = float(command[2])
result = None
if operator == "+":
result = operand1 + operand2
elif operator == "-":
result = operand1 - operand2
elif operator == "*":
result = operand1 * operand2
elif operator == "/":
if operand2 == 0:
print("Error: Division by zero is not allowed.")
return
result = operand1 / operand2
else:
print("Invalid operator. Supported operators are +, -, *, /")
return
print(f"Result: {operand1} {operator} {operand2} = {result:.2f}")