-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathBasicCalculator.py
More file actions
73 lines (64 loc) · 2.15 KB
/
Copy pathBasicCalculator.py
File metadata and controls
73 lines (64 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
"""
Basic Calculator CLI.
Usage examples:
python BasicCalculator.py add 2 3
python BasicCalculator.py divide 10 2
python BasicCalculator.py --interactive
When --interactive is used, the script prompts the user for operation and inputs.
"""
import argparse
import sys
from Functions import add, subtract, multiply, divide
OPERATIONS = {
"add": add,
"subtract": subtract,
"multiply": multiply,
"divide": divide,
}
def interactive_mode() -> int:
print("Basic Calculator (add, subtract, multiply, divide)")
op = input("Operation: ").strip().lower()
if op not in OPERATIONS:
print("Invalid operation. Choose from: add, subtract, multiply, divide")
return 2
try:
a = input("First number: ").strip()
b = input("Second number: ").strip()
result = OPERATIONS[op](a, b)
print("Result:", result)
return 0
except ZeroDivisionError as e:
print("Error:", e)
return 1
except Exception as e:
print("Error:", e)
return 1
def main(argv=None) -> int:
parser = argparse.ArgumentParser(description="Basic Calculator")
parser.add_argument("operation", nargs="?", choices=list(OPERATIONS.keys()),
help="Operation to perform")
parser.add_argument("a", nargs="?",
help="First operand (number or numeric string)")
parser.add_argument("b", nargs="?",
help="Second operand (number or numeric string)")
parser.add_argument("--interactive", action="store_true",
help="Run in interactive prompt mode")
args = parser.parse_args(argv)
if args.interactive:
return interactive_mode()
if args.operation is None or args.a is None or args.b is None:
parser.print_help()
return 2
try:
func = OPERATIONS[args.operation]
print(func(args.a, args.b))
return 0
except ZeroDivisionError as e:
print("Error:", e, file=sys.stderr)
return 1
except Exception as e:
print("Error:", e, file=sys.stderr)
return 1
if __name__ == "__main__":
raise SystemExit(main())