forked from animeshsrivastava24/PythonScriptsForBeginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculatorLambda.py
More file actions
31 lines (26 loc) · 793 Bytes
/
CalculatorLambda.py
File metadata and controls
31 lines (26 loc) · 793 Bytes
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
# 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: ')))