-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram_13_ConsoleCalculator.py
More file actions
70 lines (56 loc) · 1.99 KB
/
program_13_ConsoleCalculator.py
File metadata and controls
70 lines (56 loc) · 1.99 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#-----------------------------------------------------------------
#
class ConsoleCalculator:
def __init__(self):
self.Continue = True
self.inputLine = None
self.operation = None
self.number1 = 0.
self.number2 = 0.
self.answer = 0.
print('Welcome to the ConsoleCalculator')
print(' to exit type in q')
print(' enter equations with spaces: 2 + 2')
def ReadInput(self):
self.inputLine = input('Input > ')
if 'q' in self.inputLine:
self.Continue = False
return
# assume there a 3 input tokens: 3.4 + 4.2
# break the inputLine into separate 'words'
words = self.inputLine.split()
# make sure there are a least 3 words
if len(words) < 3:
print('Error > Not enough input')
self.number1 = 0.
self.number2 = 0.
self.operation = None
else:
self.number1 = float( words[0] )
self.operation = words[1]
self.number2 = float( words[2] )
def ComputeAnswer(self):
if self.operation == '+':
self.answer = self.number1 + self.number2
elif self.operation == '-':
self.answer = self.number1 - self.number2
elif self.operation == '*':
self.answer = self.number1 * self.number2
elif self.operation == '/':
self.answer = self.number1 / self.number2
else :
print('Error > Failed to understand the operation')
self.answer = 0.
print('Answer> ', self.answer )
#-----------------------------------------------------------------
def main():
# create (instantiate) the ConsoleCalculator
calc = ConsoleCalculator()
while True:
calc.ReadInput()
if not calc.Continue :
break
calc.ComputeAnswer()
# Tell Python to run the main() function
if __name__ == "__main__":
main()