-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.py
89 lines (74 loc) · 1.98 KB
/
Driver.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import Calc
#creating the calculator object
myCalculator = Calc.Calculator()
def isvalid_exp(expression):
"""
evaluate if an expression contains invalid character
my original
Returns : boolean
True for valid, False otherwise
output :
-------
"""
if not expression:
return False # if the expression is empty
for i in expression:
# delegate to is_valid function
if Calc.is_valid(i) or i == " ":
continue
else:
return False
return True # return True when the loop is over
def prompt():
"""
prompt user for entry
my original
Returns :
-------
output : string
prompting messages
"""
print("Welcome to Leo's calculator. Follow instructions to continue")
print("Press (e)xit to exit")
while True:
print()
expression = input("Please enter a valid expression: ")
if expression == "e":
return expression
print()
# when invalid character detected, prompt again
if not isvalid_exp(expression):
print("Invalid character entered. Please try again!!!", '\n')
continue
break
# call Calculator method
post = myCalculator.intopost(expression)
post_str = " ".join(post) # convert list to string with space
# print out the post fix expression
print("The postfix conversion of your expression is:", post_str, '\n')
result = myCalculator.evaluate(post)
# print out result
print("The expression that you entered evaluates to:", result)
print() # newline
def main():
"""
main driver that keep the program running
my original function
Returns :
-------
output : string
prompting messages
"""
while True:
result = prompt()
if result == 'e':
return
# continuation of program
print("Do you want to continue using this calculator?")
user_input = input("Please enter your choice (y)es or (n)o: ")
# continue if user enters y, terminate otherwise
if user_input == 'y':
print()
continue
else:
break