-
Notifications
You must be signed in to change notification settings - Fork 0
/
cls_calculator
46 lines (31 loc) · 1.23 KB
/
cls_calculator
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'''
'''
from collections import OrderedDict
class BinaryCalculationOperation:
def __init__(self, op):
self.op = op
def go(self):
x = self._prompt("First Number") #One underscore for private variable or private function
y = self._prompt("Second Number")
print(self.op(x,y))
def _prompt(self, prompt):
while True:
try:
return float(input(prompt))
except ValueError:
print("Make sure to enter a number .....")
def get_operation(operations):
while True:
op_name = input("What would you like to do? " +
'/'.join(operations.keys()) + ' ').title()
try:
return operations[op_name]
except KeyError:
print("That was not an option")
operations = OrderedDict([
('Multiply', BinaryCalculationOperation(lambda x, y: x * y)),
('Divide', BinaryCalculationOperation(lambda x, y: x / y)),
('Add', BinaryCalculationOperation(lambda x, y: x + y)),
('Subtract', BinaryCalculationOperation(lambda x, y: x - y)),
])
get_operation(operations).go()