/
lab02.py
194 lines (137 loc) · 4.51 KB
/
lab02.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
## Learning goals
# * Write simple functions
# * Pass information to a function via parameters
# * Pass information to a function via global variables
# * Return information from functions via return values and global variables
# * Use global variables as "static" information
def display_current_value():
'''Display the current value
'''
print('Current value:', current_value)
def get_current_value():
return current_value
def save_value():
'''Save the current value for furture use
'''
global prev_value
prev_value = current_value
def add(to_add):
'''Add to_add to the current value
Arguments:
to_add: an integer to add to the current value
'''
global current_value
save_value()
if current_value != "ERROR":
current_value += to_add
def subtract(to_subtract):
'''Subtract to_subtract from the current value
'''
global current_value
save_value()
if current_value != "ERROR":
current_value -= to_subtract
def multiply(to_mult):
'''Multiply the current value by to_mult
'''
global current_value
save_value()
if current_value != "ERROR":
current_value *= to_mult
def divide(to_divide):
'''Divide the current value by to_divide if to_divide != 0, set the current
value to 'ERROR' otherwise
'''
global current_value
save_value()
if current_value != "ERROR":
if to_divide == 0:
current_value = 'ERROR'
else:
current_value /= to_divide
def store():
'''Emulate the memory button by storing the current value for future use
Note: cannot be undone with the undo() button
'''
global mem_value
mem_value = current_value
def recall():
'''Emulate the recall button by retriving a stored memory value
'''
global current_value
current_value = mem_value
def undo():
'''Make the current value have the value it had before the last operation
'''
global current_value, prev_value
current_value, prev_value = prev_value, current_value
def initialize():
global current_value, prev_value, mem_value
current_value = 0
prev_value = 0
mem_value = 0
if __name__ == '__main__':
################################################################
#Typical cases
#Typical cases for add and subtract
#(Should also test for divide and multiply)
initialize()
add(5)
display_current_value() #expected output: 0+5=5
current_value = 15
subtract(7)
display_current_value() #expected output: 15-7=8
########################################################################
#Typical cases
#Test for interactions between the different functions
initialize()
add(5)
subtract(10)
multiply(2)
display_current_value() #expected output: (5-10)*2 = -10
#handles negative numbers?
#handles interactions between add(), subtract()...
#Other things to test for:
#undo() twice
#use both undo() and recall()
#recall() and then undo()
#undo(), recall(), add(), ... all together (maybe do a couple tests like that)
##########################################################################
#try adding negative number:
initialize()
add(-5) #expected output: 0-5=-5
#try an "irrational" number:
import math
current_value = 42
divide(math.pi)
display_current_value() #expected value: approx. 13.36901521971921
##########################################################################
#Boundary case:
#Something that involves zero
initialize()
add(5)
subtract(5)
display_current_value() # expected: 5-5 = 0
#try adding zero:
current_value = 5
add(0) #expected output: 5+0=0
###########################################################################
#Error cases:
#try dividing by 0
current_value = 10
divide(0)
display_current_value() #expected output: ERROR
#produce an error, then try adding 10
current_value = 10
divide(0)
add(5) #expected output: you should decide
###########################################################################
#Special cases where you might expect problems
#Divide by a very large number (so as to get 0), then multiply by the same
#number
#add a very large number to another large number, see if there's a limit
#to the number of digits
#divide 0 by 0
#can you undo an "ERROR"?
#can you store "ERROR"
#try adding 5 to "ERROR"