Skip to content

Commit d986df0

Browse files
committed
Added main program
1 parent ccf91e4 commit d986df0

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
# moses-shares
1+
# Summenberechnung
2+
3+
Usage:
4+
```
5+
Usage: python main.py num num [num ...]
6+
7+
<num> can be either float or integer
8+
minimum amount of arguments is two
9+
```
210

3-
Ein Repo zu Übungszwecken ... :)

main.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sys
2+
import re
3+
4+
5+
def parse_str(num):
6+
"""
7+
Parse a string that is expected to contain a number.
8+
:param num: str. the number in string.
9+
:return: float or int. Parsed num.
10+
"""
11+
if not isinstance(num, str): # optional - check type
12+
raise TypeError('num should be a str. Got {}.'.format(type(num)))
13+
if re.compile('^\s*\d+\s*$').search(num):
14+
return int(num)
15+
if re.compile('^\s*(\d*\.\d+)|(\d+\.\d*)\s*$').search(num):
16+
return float(num)
17+
raise ValueError('num is not a number. Got {}.'.format(num)) # optional
18+
19+
20+
def sum_all(values: list) -> int:
21+
summary = 0
22+
for val in values:
23+
_v = parse_str(val)
24+
summary += _v
25+
return summary
26+
27+
if len(sys.argv) < 3:
28+
usage = '''Usage: python main.py num num [num ...]
29+
30+
<num> can be either float or integer
31+
minimum amount of arguments is two
32+
'''
33+
print(usage)
34+
sys.exit(1)
35+
36+
summary = sum_all(sys.argv[1:])
37+
38+
print(f"Die Summe ist: {summary}")

0 commit comments

Comments
 (0)