-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcalc.py
More file actions
executable file
·65 lines (48 loc) · 1.33 KB
/
calc.py
File metadata and controls
executable file
·65 lines (48 loc) · 1.33 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
#!/usr/bin/env python3
"""Berechnet Dinge."""
from argparse import ArgumentParser
def op_add(a: int, b: int) -> int:
"""Addiert a und b."""
return a + b
def op_sub(a: int, b: int) -> int:
"""Subtrahiert b von a."""
return a - b
def op_mult(a: int, b: int) -> int:
"""Multipliziert a mit b."""
return a * b
def op_div(a: int, b: int) -> float:
"""Teilt a durch b."""
return a / b
def op_exp(a: int, b: int) -> int:
"""Potenziert a mit b."""
return a ** b
def op_root(a: int, b: int) -> float:
"""Berechnet die a-te Wurzel von b."""
return b ** (1 / a)
parser = ArgumentParser(description=__doc__)
subparsers = parser.add_subparsers(
dest="command",
help="die auszuführende Rechenoperation"
)
subparsers.required = True
for function in (
op_add,
op_sub,
op_mult,
op_div,
op_exp,
op_root,
):
parser_func = subparsers.add_parser(
function.__name__.lstrip("op_"),
help=function.__doc__
)
parser_func.add_argument("a", help="erster Wert", type=int)
parser_func.add_argument("b", help="zweiter Wert", type=int)
parser_func.set_defaults(func=function)
if __name__ == "__main__":
# Verarbeiten der Argumente
args = parser.parse_args()
# die tatsächliche Funktion aufrufen
res = args.func(args.a, args.b)
print(res)