|
| 1 | +def formatStr(selector, *args): |
| 2 | + text_templates = [ |
| 3 | + 'Geben Sie den {0} Wert: ', |
| 4 | + 'Ergebnis der {0} von {1} {2} {3} = {4}' |
| 5 | + ] |
| 6 | + |
| 7 | + if selector == 0: |
| 8 | + value_name = args[0] |
| 9 | + return text_templates[selector].format(value_name) |
| 10 | + elif selector == 1: |
| 11 | + operation, num1, operator, num2, result = args |
| 12 | + return text_templates[selector].format(operation, num1, operator, num2, result) |
| 13 | + else: |
| 14 | + return None |
| 15 | + |
| 16 | +def formatPrint(selector, *args): |
| 17 | + print(formatStr(selector, *args)) |
| 18 | + |
| 19 | +def two_decimal(val): |
| 20 | + return f'{val:.2f}' |
| 21 | + |
| 22 | +try: |
| 23 | + wert_01 = float(input(formatStr(0, 'erste'))) |
| 24 | + wert_02 = float(input(formatStr(0, 'zweite'))) |
| 25 | +except ValueError: |
| 26 | + print('Sie sollten nur natürlichen Zahlen eingeben!!!') |
| 27 | +else: |
| 28 | + startKey = 0 |
| 29 | + operation_name = ['Addition', 'Substraktion', 'Multiplikation', 'Division'] |
| 30 | + operation_symbol = ['+', '-', '*', '/'] |
| 31 | + try: |
| 32 | + operation_result = [wert_01 + wert_02, wert_01 - wert_02, wert_01 * wert_02, wert_01 / wert_02] |
| 33 | + except ZeroDivisionError: |
| 34 | + print('Wert 2 darf niemals als 0 sein!') |
| 35 | + else: |
| 36 | + wert_01 = two_decimal(wert_01) |
| 37 | + wert_02 = two_decimal(wert_02) |
| 38 | + |
| 39 | + for name in operation_name: |
| 40 | + formatPrint(1, name, wert_01, operation_symbol[startKey], wert_02, two_decimal(operation_result[startKey])) |
| 41 | + startKey += 1 |
0 commit comments