-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
83 lines (64 loc) · 2.22 KB
/
main.py
File metadata and controls
83 lines (64 loc) · 2.22 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
74
75
76
77
78
79
80
81
82
83
import argparse
from typing import Callable, Dict
OPERATIONS: Dict[str, Callable[[float, float], float]] = {
"add": lambda left, right: left + right,
"subtract": lambda left, right: left - right,
"multiply": lambda left, right: left * right,
"divide": lambda left, right: left / right,
}
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Simple calculator for terminal-based math operations."
)
parser.add_argument("--left", type=float, help="First number.")
parser.add_argument("--right", type=float, help="Second number.")
parser.add_argument(
"--operation",
choices=sorted(OPERATIONS.keys()),
help="Operation to apply.",
)
return parser.parse_args()
def ask_number(label: str) -> float:
while True:
raw_value = input(f"{label}: ").strip()
try:
return float(raw_value)
except ValueError:
print("Enter a valid number.")
def ask_operation() -> str:
options = {
"1": "add",
"2": "subtract",
"3": "multiply",
"4": "divide",
}
print("\nChoose an operation")
print("1 - Add")
print("2 - Subtract")
print("3 - Multiply")
print("4 - Divide")
while True:
choice = input("Option: ").strip()
if choice in options:
return options[choice]
print("Choose one of the listed options.")
def calculate(left: float, right: float, operation: str) -> float:
if operation == "divide" and right == 0:
raise ZeroDivisionError("Division by zero is not allowed.")
return OPERATIONS[operation](left, right)
def main() -> None:
args = parse_args()
left = args.left if args.left is not None else ask_number("First number")
right = args.right if args.right is not None else ask_number("Second number")
operation = args.operation or ask_operation()
try:
result = calculate(left, right, operation)
except ZeroDivisionError as error:
print(f"Error: {error}")
raise SystemExit(1) from error
print("\nCalculation result")
print("------------------")
print(f"Operation: {operation}")
print(f"Result: {result}")
if __name__ == "__main__":
main()