From 7c83a1d5cf4a0e0c2523203f50e4841ecb405940 Mon Sep 17 00:00:00 2001 From: andgorbachov Date: Wed, 9 Dec 2020 19:56:35 +0200 Subject: [PATCH 1/2] simple Calculator with Lamba functions --- CalculatorLambda.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 CalculatorLambda.py diff --git a/CalculatorLambda.py b/CalculatorLambda.py new file mode 100644 index 0000000..e69de29 From e2567e4eebca00b6479638a1f656aa0fac95fceb Mon Sep 17 00:00:00 2001 From: andgorbachov Date: Wed, 9 Dec 2020 20:05:14 +0200 Subject: [PATCH 2/2] simple Calculator with Lambda functions --- CalculatorLambda.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/CalculatorLambda.py b/CalculatorLambda.py index e69de29..886acb4 100644 --- a/CalculatorLambda.py +++ b/CalculatorLambda.py @@ -0,0 +1,31 @@ +# Application makes a simple calculator with lambda function + + +# The function performs calculation +def calculate(entered_data): + x_number = '' + y_number = '' + operator = None + for char in entered_data: + if char.isdigit(): + if operator is None: + x_number += char + else: + y_number += char + elif char in operations: + operator = char + elif char.isspace: + pass + else: + raise Exception('invalid operator: ' + char) + return operations[operator](int(x_number), int(y_number)) + + +operations = { + '+': lambda x, y: x + y, + '-': lambda x, y: x - y, + '*': lambda x, y: x * y, + '/': lambda x, y: x / y +} + +print(calculate(input('Input what you want to calculate: ')))