File tree 2 files changed +47
-2
lines changed
2 files changed +47
-2
lines changed Original file line number Diff line number Diff line change 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
+ ```
2
10
3
- Ein Repo zu Übungszwecken ... :)
Original file line number Diff line number Diff line change
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 } " )
You can’t perform that action at this time.
0 commit comments