Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
import argparse
import sys

def _format_result(value):
# If value is a float that represents an integer, print as int for clean output
try:
if isinstance(value, float) and value.is_integer():
return str(int(value))
except Exception:
pass
return str(value)

def add(a, b):
return a + b

def subtract(a, b):
return a - b

def multiply(a, b):
return a * b

def divide(a, b):
if b == 0:
raise ZeroDivisionError("division by zero")
return a / b

def main():
parser = argparse.ArgumentParser(prog="calculator", description="Basic math operations")
subparsers = parser.add_subparsers(dest="operation", required=True)

for name in ["add", "subtract", "multiply", "divide"]:
sp = subparsers.add_parser(name)
sp.add_argument("a", type=float)
sp.add_argument("b", type=float)

args = parser.parse_args()

op = args.operation
a = args.a
b = args.b

if op == "add":
res = add(a, b)
elif op == "subtract":
res = subtract(a, b)
elif op == "multiply":
res = multiply(a, b)
elif op == "divide":
res = divide(a, b)
else:
parser.error("Unknown operation")

print(_format_result(res))

if __name__ == "__main__":
main()