-
Notifications
You must be signed in to change notification settings - Fork 0
/
SigFigsBlueprints.py
479 lines (444 loc) · 24.6 KB
/
SigFigsBlueprints.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import random
from flask import Flask, request, redirect, render_template, session, flash, Blueprint
import cgi
from SigFigFuncs import MakeNumber, RoundValue, CheckAnswer, CheckRounding, ApplySciNotation, addValues, subtractValues, multiplyValues, divideValues, findDecimalPlaces, addWithPlaceholders, subtractWithPlaceholders
sigfigs_blueprint = Blueprint('sigfigs_blueprint',__name__)
@sigfigs_blueprint.route('/sigfigindex')
def sigfigindex():
return render_template('sigfigindex.html',title="Sig Fig Practice")
@sigfigs_blueprint.route('/countingsf', methods=['POST', 'GET'])
def countingsf():
if request.method == 'POST':
answer = request.form['answer']
actualSigFigs = request.form['actualSigFigs']
value = request.form['value']
if answer==actualSigFigs:
flash('Correct! :-)', 'correct')
else:
flash('Try again, or click here to reveal the answer.', 'error')
return render_template('countingSigFigs.html', value=value, sigFigs = actualSigFigs, answer = answer)
sigFigs = random.randrange(1,7)
power = random.randrange(-5,9)
value = MakeNumber(sigFigs,power)
return render_template('countingSigFigs.html',title="Counting Sig Figs", value=value, sigFigs = sigFigs)
@sigfigs_blueprint.route('/roundingsf', methods=['POST', 'GET'])
def roundingsf():
if request.method == 'POST':
answer = request.form['answer']
origValue = request.form['value']
sigFigs = int(request.form['sigFigs'])
roundedValue = RoundValue(origValue, sigFigs)
if CheckAnswer(roundedValue, answer):
flash('Correct! :-)', 'correct')
else:
flash('Try again, or click here to reveal the answer.', 'error')
return render_template('roundingSigFigs.html', value=origValue, sigFigs = sigFigs, answer = answer, roundedValue=roundedValue)
iffyValue = True
while iffyValue:
sigFigs = random.randrange(1,7)
power = random.randrange(-4,6)
value = MakeNumber(9,power)
result = RoundValue(value, sigFigs)
iffyValue = CheckRounding(result,sigFigs)
return render_template('roundingSigFigs.html',title="Rounding Sig Figs", value=value, sigFigs = sigFigs)
@sigfigs_blueprint.route('/sfcalcs', methods=['POST', 'GET'])
def sfcalcs():
if request.method == 'POST':
answer = request.form['answer']
result = request.form['result']
value1 = request.form['value1']
value2 = request.form['value2']
operation = request.form['operation']
if CheckAnswer(result, answer):
flash('Correct! :-)', 'correct')
else:
flash('Try again, or click here to reveal the answer.', 'error')
return render_template('sfCalcs.html',title="Calculations with Sig Figs", value1 = value1, value2 = value2, result = result, answer = answer, operation=operation)
operators = ['+', '-', 'x', '/']
operation = random.randrange(4) #Randomly select +, -, * or / using integers 0 - 3, respectively.
if operation < 2: #For + and -, create 2 values between 0.001 and 90 with 1 - 6 sig figs.
iffyValue = True
while iffyValue:
sigFigs = random.randrange(1,7)
power = random.randrange(-3,2)
value = MakeNumber(sigFigs,power)
iffyValue = CheckRounding(value,sigFigs)
sigFigs1 = sigFigs
power1 = power
value1 = value
iffyValue = True
while iffyValue:
sigFigs = random.randrange(1,7)
power = random.randrange(-3,2)
value = MakeNumber(sigFigs,power)
iffyValue = CheckRounding(value,sigFigs)
sigFigs2 = sigFigs
power2 = power
value2 = value
else: #For * and /, create 2 values between 0.01 and 900 with 1 - 6 sig figs.
sigFigs1 = random.randrange(1,7)
power1 = random.randrange(-2,3)
value1 = MakeNumber(sigFigs1,power1)
sigFigs2 = random.randrange(1,7)
power2 = random.randrange(-2,3)
value2 = MakeNumber(sigFigs2,power2)
if operation == 0:
if (float(value1)>=10 and value1.find('.') == -1 and sigFigs1 < len(value1)) or (float(value2)>=10 and value2.find('.') == -1 and sigFigs2 < len(value2)):
result = addWithPlaceholders(value1,value2)
else:
result = addValues(value1,value2)
return render_template('sfCalcs.html',title="Calculations with Sig Figs", value1 = value1, value2 = value2, operation = operators[operation], result = result)
elif operation == 1 and value1 > value2:
if (float(value1)>=10 and value1.find('.') == -1 and sigFigs1 < len(value1)) or (float(value2)>=10 and value2.find('.') == -1 and sigFigs2 < len(value2)):
result = subtractWithPlaceholders(value1,value2)
else:
result = subtractValues(value1,value2)
return render_template('sfCalcs.html',title="Calculations with Sig Figs", value1 = value1, value2 = value2, operation = operators[operation], result = result)
elif operation == 1 and float(value1) < float(value2):
if (float(value1)>=10 and value1.find('.') == -1 and sigFigs1 < len(value1)) or (float(value2)>=10 and value2.find('.') == -1 and sigFigs2 < len(value2)):
result = subtractWithPlaceholders(value2,value1)
else:
result = subtractValues(value2,value1)
return render_template('sfCalcs.html',title="Calculations with Sig Figs", value1 = value2, value2 = value1, operation = operators[operation], result = result)
elif operation == 2:
result = multiplyValues(value1,sigFigs1,value2,sigFigs2)
return render_template('sfCalcs.html',title="Calculations with Sig Figs", value1 = value1, value2 = value2, operation = operators[operation], result = result)
elif float(value1)/float(value2)<1e-4:
result = divideValues(value2,sigFigs2,value1,sigFigs1)
return render_template('sfCalcs.html',title="Calculations with Sig Figs", value1 = value2, value2 = value1, operation = operators[operation], result = result)
else:
result = divideValues(value1,sigFigs1,value2,sigFigs2)
return render_template('sfCalcs.html',title="Calculations with Sig Figs", value1 = value1, value2 = value2, operation = operators[operation], result = result)
@sigfigs_blueprint.route('/scinotation', methods=['POST', 'GET'])
def scinotation():
if request.method == 'POST':
sciNot = request.form['sciNot']
if sciNot=='True': #Given a value in sci notation, the user eneters a number in standard notation.
answer = request.form['answer']
result = request.form['value']
sciValue = request.form['sciValue']
power = request.form['power']
if CheckAnswer(result, answer):
flash('Correct! :-)', 'correct')
else:
flash('Try again, or click here to reveal the answer.', 'error')
return render_template('scientificNotation.html',title="Scientific Notation", value = result, sciValue=sciValue, power = power, sciNot = True, answer = answer)
else: #Given a value in standard notation, the user eneters a number in sci notation.
answer = request.form['answer']
result = request.form['value']
sciValue = request.form['sciValue']
power = request.form['power']
exponent = request.form['exponent']
if CheckAnswer(power, exponent) and CheckAnswer(sciValue,answer):
flash('Correct! :-)', 'correct')
elif CheckAnswer(power, exponent) and not CheckAnswer(sciValue,answer):
flash('Correct power. Wrong decimal value.', 'error')
elif CheckAnswer(sciValue,answer) and not CheckAnswer(power, exponent):
flash('Correct decimal value. Wrong power.', 'error')
else:
flash('Both entries are incorrect. Try again, or click to reveal the answer.', 'error')
return render_template('scientificNotation.html',title="Scientific Notation", value = result, sciValue=sciValue, power = power, sciNot = False, answer = answer, exponent = exponent)
sigFigs = random.randrange(1,5)
power = random.randrange(-5,9)
value = MakeNumber(sigFigs,power)
sciValue = ApplySciNotation(value, sigFigs)
if random.randrange(2) == 0: #Flip a coin: If '0', ask the user to change sci notation into standard notation.
return render_template('scientificNotation.html',title="Scientific Notation", value = value, sciValue=sciValue, power = power, sciNot = True)
else: #Otherwise ('1'), ask the user to change standard notation into sci notation.
return render_template('scientificNotation.html',title="Scientific Notation", value=value, sciValue=sciValue, power = power, sciNot = False)
@sigfigs_blueprint.route('/sftutorial/<type>', methods=['POST', 'GET'])
def sftutorial(type):
page = int(type)
if page == 1 or page == 5:
if request.method == 'POST':
displayText = int(request.form['displayText'])
displayText += 1
else:
displayText=1
return render_template('sftutorial.html',title="Sig Fig Tutorial", page = page, displayText=displayText)
elif page == 2:
if request.method == 'POST':
firstZeroRule = request.form['firstZeroRule']
session['firstZeroRule'] = firstZeroRule
secondHalf = True
if firstZeroRule == '':
flash('Please enter a response.', 'error')
secondHalf = False
return render_template('sftutorial.html', answer = firstZeroRule, page = page, secondHalf = secondHalf)
return render_template('sftutorial.html',title="Sig Fig Tutorial", page = page, secondHalf=False)
elif page == 3:
firstZeroRule = session.get('firstZeroRule', None)
if request.method == 'POST':
secondZeroRule = request.form['secondZeroRule']
session['secondZeroRule'] = secondZeroRule
secondHalf = True
if secondZeroRule == '':
flash('Please enter a response.', 'error')
secondHalf = False
return render_template('sftutorial.html', firstZeroRule = firstZeroRule, secondZeroRule = secondZeroRule, page = page, secondHalf = secondHalf)
return render_template('sftutorial.html',title="Sig Fig Tutorial", page = page, firstZeroRule = firstZeroRule, secondHalf=False)
else: #Counting sig figs tutorial page 4
firstZeroRule = session.get('firstZeroRule', None)
secondZeroRule = session.get('secondZeroRule', None)
return render_template('sftutorial.html',title="Sig Fig Tutorial", page = page, firstZeroRule=firstZeroRule, secondZeroRule=secondZeroRule)
@sigfigs_blueprint.route('/roundingtutorial/<type>', methods=['POST', 'GET'])
def roundingtutorial(type):
page = int(type)
if page == 1 or page == 4:
return render_template('roundingtutorial.html',title="Rounding Tutorial", page = page)
elif page == 2:
answers = []
numCorrect = 0
if request.method == 'POST':
displayText = int(request.form['displayText'])
displayText += 1
roundedAnswer = request.form['5SigFigs']
if displayText == 4 and roundedAnswer != '12.386':
flash('Not quite correct. Try again.', 'error')
displayText = 3
elif displayText>5:
correctAnswers = ['0.00798','0.0080','0.008']
for x in range(3):
answers.append(request.form[str(3-x)+'SigFigs'])
if CheckAnswer(correctAnswers[x],answers[x]):
flash('Correct! :-)', 'correct')
numCorrect += 1
else:
flash('Try again.', 'error')
else:
displayText=1
roundedAnswer = ''
return render_template('roundingtutorial.html',title="Rounding Tutorial", page = page, displayText=displayText, roundedAnswer = roundedAnswer, answers = answers, numCorrect = numCorrect)
else: #3rd page of rounding to sig figs tutorial
answers = []
numCorrect = 0
if request.method == 'POST':
displayText = int(request.form['displayText'])
displayText += 1
example3 = request.form['example3']
if displayText == 2 and example3 != '2380':
flash('Not quite correct. Try again.', 'error')
displayText = 1
elif displayText > 3:
correctAnswers = ['0.0998','0.10','0.1']
for x in range(3):
answers.append(request.form[str(3-x)+'SigFigs'])
if CheckAnswer(correctAnswers[x],answers[x]):
flash('Correct! :-)', 'correct')
numCorrect += 1
else:
flash('Try again.', 'error')
else:
displayText=1
example3 = ''
return render_template('roundingtutorial.html',title="Rounding Tutorial", page = page, displayText=displayText, answers = answers, example3 = example3, numCorrect = numCorrect)
@sigfigs_blueprint.route('/scinottutorial/<type>', methods=['POST', 'GET'])
def scinottutorial(type):
page = int(type)
if page == 1:
if request.method == 'POST':
displayText = int(request.form['displayText'])
displayText += 1
if displayText == 2:
decimal = request.form['decimal']
power = request.form['exponent']
decimals = ['1.5', '15', '150', '1500']
powers = ['3','2','1','0']
if decimal in decimals:
index = decimals.index(decimal)
if power != powers[index]:
flash('Incorrect power. Try again.', 'error')
displayText = 1
else:
flash('Incorrect decimal value. Try again.', 'error')
displayText = 1
else:
decimal = ''
power = ''
else:
displayText=1
decimal = ''
power = ''
return render_template('scinottutorial.html',title="Scientific Notation Tutorial", page = page, displayText = displayText, decimal = decimal, exponent=power)
elif page == 2:
decimals = []
powers = []
exponents = []
values = []
sciValues = []
numCorrect = 0
if request.method == 'POST':
for item in range(4):
decimals.append(request.form['decimal'+str(item)])
exponents.append(request.form['exponent'+str(item)])
values.append(request.form['value'+str(item)])
powers.append(request.form['power'+str(item)])
sciValues.append(request.form['sciValue'+str(item)])
if CheckAnswer(powers[item], exponents[item]) and CheckAnswer(sciValues[item],decimals[item]):
flash('Correct! :-)', 'correct')
numCorrect += 1
elif CheckAnswer(powers[item], exponents[item]) and not CheckAnswer(sciValues[item],decimals[item]):
flash('Correct power. Wrong decimal value.', 'error')
elif CheckAnswer(sciValues[item],decimals[item]) and not CheckAnswer(powers[item], exponents[item]):
flash('Correct decimal value. Wrong power.', 'error')
else:
flash('Both entries are incorrect. Try again.', 'error')
else:
for item in range(4):
sigFigs = random.randrange(1,5)
if item <= 1:
power = random.randrange(0,7)
else:
power = random.randrange(-5,0)
value = MakeNumber(sigFigs,power)
values.append(value)
powers.append(power)
sciValues.append(ApplySciNotation(value, sigFigs))
return render_template('scinottutorial.html',title="Scientific Notation Tutorial", page = page, values = values, decimals = decimals, exponents = exponents, sciValues = sciValues, powers = powers, numCorrect = numCorrect)
elif page == 3:
values = []
sciValues = []
powers = []
answers = []
numCorrect = 0
if request.method == 'POST':
for item in range(4):
answers.append(request.form['answer'+str(item)])
values.append(request.form['value'+str(item)])
powers.append(request.form['power'+str(item)])
sciValues.append(request.form['sciValue'+str(item)])
if ',' in answers[item]:
flash('Please remove the comma(s) from your answer.', 'error')
elif CheckAnswer(values[item], answers[item]):
flash('Correct! :-)', 'correct')
numCorrect += 1
else:
flash('Oops! Try again.', 'error')
else:
for item in range(4):
sigFigs = random.randrange(1,5)
if item <= 1:
power = random.randrange(0,7)
else:
power = random.randrange(-5,0)
powers.append(power)
values.append(MakeNumber(sigFigs,power))
sciValues.append(ApplySciNotation(values[item], sigFigs))
return render_template('scinottutorial.html',title="Scientific Notation Tutorial", page = page, values = values, answers = answers, sciValues = sciValues, powers = powers, numCorrect = numCorrect)
else:
return render_template('scinottutorial.html',title="Scientific Notation Tutorial", page = page)
@sigfigs_blueprint.route('/sfcalcstutorial/<type>', methods=['POST', 'GET'])
def sfcalcstutorial(type):
page = int(type)
if page == 1:
if request.method == 'POST':
imageText = ['Assume two students measure the length of a small tile. Their results are not the same, but the difference is small in this case.',
'To predict the length of two tiles, the students simply double their measurements. Note that the difference (uncertainty) in their results is LARGER than before.',
'For five tiles, something interesting happens with the error in the predicted lengths.',
'The difference between the results becomes too large to keep 2 decimal places, so the guess digit moves into the tenths place.',
'What if each student calculated the area of the tile?','Multiplying measurements also increases error.','Since two digits are now uncertain, we must round each answer to maintain a single guess digit.']
displayText = int(request.form['displayText']) + 1
imageName = 'SFCalcs'+str(displayText-1)+'.png'
return render_template('sfcalcstutorial.html',title="Calculations with Sig Figs Tutorial", page = page, displayText = displayText, imageText = imageText, imageName = imageName)
displayText = 1
return render_template('sfcalcstutorial.html',title="Calculations with Sig Figs Tutorial", page = page, displayText = displayText)
elif page == 2:
answers = []
results = []
values = []
numCorrect = 0
if request.method == 'POST':
for item in range(4):
values.append(request.form['value'+str(item)])
if item < 2:
answers.append(request.form['answer'+str(item)])
results.append(request.form['result'+str(item)])
if CheckAnswer(results[item], answers[item]):
flash('Correct! :-)', 'correct')
numCorrect += 1
else:
flash('Try again, or click to see the answer.', 'error')
return render_template('sfcalcstutorial.html',title="Calculations with Sig Figs Tutorial", page = page, values = values, answers = answers, results = results, numCorrect = numCorrect)
else:
sigFigs = []
powers = []
for index in range(4):
sigFigs.append(random.randrange(1,7))
powers.append(random.randrange(-2,3))
values.append(MakeNumber(sigFigs[index],powers[index]))
flip = False
if index == 1:
results.append(multiplyValues(values[index-1],sigFigs[index-1],values[index],sigFigs[index]))
elif index == 3 and float(values[index-1])/float(values[index])<1e-4:
temp = values[index-1]
values[index-1]=values[index]
values[index]=temp
results.append(divideValues(values[index-1],sigFigs[index-1],values[index],sigFigs[index]))
elif index == 3:
results.append(divideValues(values[index-1],sigFigs[index-1],values[index],sigFigs[index]))
return render_template('sfcalcstutorial.html',title="Calculations with Sig Figs Tutorial", page = page, values = values, sigFigs = sigFigs, powers = powers, answers = answers, flip = flip, results = results, numCorrect = numCorrect)
elif page == 3:
answers = []
results = []
values = []
numCorrect = 0
if request.method == 'POST':
for item in range(4):
values.append(request.form['value'+str(item)])
if item < 2:
answers.append(request.form['answer'+str(item)])
results.append(request.form['result'+str(item)])
if CheckAnswer(results[item], answers[item]):
flash('Correct! :-)', 'correct')
numCorrect += 1
else:
flash('Try again, or click to see the answer.', 'error')
return render_template('sfcalcstutorial.html',title="Calculations with Sig Figs Tutorial", page = page, values = values, answers = answers, results = results, numCorrect = numCorrect)
else:
sigFigs = []
powers = []
while len(values) < 4:
iffyValue = True
while iffyValue:
sigFig = random.randrange(1,7)
power = random.randrange(-3,2)
value = MakeNumber(sigFig,power)
iffyValue = CheckRounding(value,sigFig)
sigFigs.append(sigFig)
powers.append(power)
values.append(value)
if (float(values[0])>=10 and values[0].find('.') == -1 and sigFigs[0] < len(values[0])) or (float(values[1])>=10 and values[1].find('.') == -1 and sigFigs[1] < len(values[1])):
results.append(addWithPlaceholders(values[0],values[1]))
else:
results.append(addValues(values[0],values[1]))
if float(values[2]) < float(values[3]):
values[2],values[3] = values[3],values[2]
sigFigs[2],sigFigs[3] = sigFigs[3],sigFigs[2]
if (float(values[2])>=10 and values[2].find('.') == -1 and sigFigs[2] < len(values[2])) or (float(values[3])>=10 and values[3].find('.') == -1 and sigFigs[3] < len(values[3])):
results.append(subtractWithPlaceholders(values[2],values[3]))
else:
results.append(subtractValues(values[2],values[3]))
return render_template('sfcalcstutorial.html',title="Calculations with Sig Figs Tutorial", page = page, values = values, sigFigs = sigFigs, powers = powers, answers = answers, results = results, numCorrect = numCorrect)
else:
if request.method == 'POST':
displayText = int(request.form['displayText'])
response = int(request.form['response'])
example = int(request.form['example'])
if displayText == 0 and response < 2:
answer = request.form['answer']
if example == 0 and answer=='3':
response += 1
else:
flash('This is NOT a trick question. Count again...', 'error')
elif displayText <= 2 and response < 1:
response += 1
example += 1
else:
response = 0
example += 1
displayText += 1
else:
displayText = 0
example = 0
response = 0
return render_template('sfcalcstutorial.html',title="Calculations with Sig Figs Tutorial", page = page, displayText = displayText, example = example, response = response)