-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion.py
1695 lines (1458 loc) · 69.4 KB
/
Question.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from wordData import *
from tkExtensions import *
from sequenceGenerator import SequenceGenerator
from constants import *
import random
from commonFunctions import *
#Set the wordData object
wordData = None
def setWordData(data):
global wordData
wordData = data
#Question base class
class Question:
def __init__(self, daysToComplete = None):
self.answer = ""
self.wordsToLookup = []
self.correct = False
self.dateSet = datetime.datetime.now()
self.correctAnswer = ""
self.submitButtonSettings = {
"hoverColour":BUTTONACTIVEBACKGROUND,
"hoverText":BUTTONACTIVETEXT,
"fg":BUTTONIDLETEXT,
"bg":BUTTONIDLEBACKGROUND,
"activeforeground":BUTTONCLICKEDTEXT,
"activebackground":BUTTONCLICKEDBACKGROUND,
"bd":BUTTONBORDERWIDTH,
"highlightbackground":BUTTONBORDER,
"relief":"solid",
"width":10,
"height":1,
"font":(FONT, 15)
}
if daysToComplete is not None:
self.dateDue = self.dateSet + ONEDAY
else:
self.dateDue = None
#Generate the question
def generate(self):
pass
#Check if the answer was correct
def verify(self, answer):
self.answer=answer
return answer != ""
#Wrapper display function
def display(self, endFunction, parent, submitNow = True):
pass
#View the question and its result after doing it
def view(self, useragent, parent):
pass
#A question about completeing sequences
class SequenceQuestion(Question):
def __init__(self, min = -100, max = 100, step = 10, maxEndStep = 100, maxStepChange = 10, maxMultiDivisionFactor = 10, minDegree = 2, maxDegree = 10, minStartValue = -10, maxStartValue = 10):
#kwargs for the sequence generator
self.kwargs = {"min":min,
"max":max,
"step":step,
"maxEndStep":maxEndStep,
"maxStepChange":maxStepChange,
"maxMultiDivisionFactor":maxMultiDivisionFactor,
"minDegree":minDegree,
"maxDegree":maxDegree,
"minStartValue":minStartValue,
"maxStartValue":maxStartValue
}
super().__init__()
self.sequence = []
self.nextTerm = 0
self.timeToForm = 0
self.answer = None
self.correctAnswer = None
self.correct = False
self.options = []
self.seqtype = None
self.typeExplaination = None #Explains what type of sequence is used
self.type = "Complete the sequence" #Title of the question
self.generate()
#Generate the question
def generate(self):
s = SequenceGenerator(**self.kwargs) #Create a new sequence generator
fullsequence = []
while len(fullsequence) < 10: #Get a sequence with a length less than 10
fullsequence, self.seqtype = s.random() #Generate a random sequence
self.typeExplaination = s.typeDescriptions[self.seqtype] #Get an explanation of the type of sequence
sequenceLength = randomNumber(5,7) #Generate a final sequence length
sequenceStart = randomNumber(0,len(fullsequence)-(sequenceLength + 2)) #Generate a position in the full sequence to start the final sequence
self.sequence = []
i = None
#Generate the final sequence
for i in range(sequenceStart, sequenceStart + sequenceLength):
self.sequence.append(fullsequence[i])
self.correctAnswer = fullsequence[i + 1] #Get the correct answer
#Generate the options for the multiple choice question
#Set the bounds for the options
minValue = self.sequence[0]
maxValue = self.sequence[-1]
if minValue > maxValue:
temp = minValue
minValue = maxValue
maxValue = temp
#Get the options
self.options = [self.correctAnswer]
for i in range(4):
x = randomNumber(minValue, maxValue, exclude = self.options)
while x == (minValue - 1):
minValue -= 1
x = randomNumber(minValue, maxValue, exclude = self.options)
self.options.append(x)
random.shuffle(self.options)
#Check if the answer was correct
def verify(self, answer):
if not super().verify(answer):
return False
return answer == str(self.correctAnswer)
#Display the question on android
def displayAndroid():
pass
#Display the question on windows
def displayWindows(self, endFunction, parent, submitNow = True):
#Question Description
tk.Label(parent, text = "In each question, find the number that continues the series in the most sensible way and write it in the box", font=(FONT, 13, "bold", "underline")).pack(padx=5, pady=5)
tk.Label(parent, text=", ".join([str(s) for s in self.sequence]), font=(FONT, 12, "bold")).pack(padx=5, pady=5)
optionsFrame = tk.Frame(parent)
for i in range(len(self.options)):
tk.Label(optionsFrame, text=str(self.options[i]), font=(FONT, 12)).grid(row = 0, column = i, padx=5, pady=5)
optionsFrame.pack(padx=5, pady=5)
#Answer box
answerBox = tk.Entry(parent, font=(FONT, 12))
answerBox.pack(padx=5, pady=5)
submitButton = None
#Show the result after marking
def showResult():
if answerBox.get() != "":
self.correct = self.verify(answerBox.get()) #Mark the question
#Display the result
if(self.correct):
tk.Label(parent, text="Correct!", font=(FONT, 12)).pack(padx=5, pady=5)
else:
tk.Label(parent, text="Incorrect :( The right answer was: '" + str(self.correctAnswer) + "'", font=(FONT, 12)).pack(padx=5, pady=5)
submitButton.destroy()
endFunction(self, [], self.type)
#Create the submit button if the question is to be marked immediately after doing it
if submitNow:
submitButton = HoverButton(parent, text = "Submit", command=showResult, **self.submitButtonSettings)
submitButton.pack(padx=5, pady=5)
parent.pack()
return answerBox
#Wrapper display function
def display(self, useragent, *args, **kwargs):
if useragent == ANDROID:
self.displayAndroid()
else:
self.displayWindows(*args, **kwargs)
#View the question and its result after doing it
def view(self, useragent, parent):
tk.Label(parent, text = "In each question, find the number that continues the series in the most sensible way and write it in the box", font=(FONT, 13, "bold", "underline")).pack(padx=5, pady=5)
tk.Label(parent, text=", ".join([str(s) for s in self.sequence]), font=(FONT, 12, "bold")).pack(padx=5, pady=5)
optionsFrame = tk.Frame(parent)
for i in range(len(self.options)):
tk.Label(optionsFrame, text=str(self.options[i]), font=(FONT, 12)).grid(row = 0, column = i, padx=5, pady=5)
optionsFrame.pack(padx=5, pady=5)
tk.Label(parent, text="Answer of '" + str(self.correctAnswer) + "' gives the sequence '" + ", ".join([str(s) for s in self.sequence] + [str(self.correctAnswer)]) + "'\nThe type of sequence is " + self.seqtype + ". This sequence is solved like this:\n" + self.typeExplaination ).pack(padx=5, pady=5)
parent.pack()
#A question were the student must put the same letter to complete 2 words and start 2 others
class SameLetterFourWordsQuestion(Question):
def __init__(self, minLength = 4, maxLength = 8):
super().__init__()
self.word1 = ""
self.word2 = ""
self.word3 = ""
self.word4 = ""
self.letter = ""
self.type = "Same letter four words" #Title of the question
self.timeToForm = 0
self.generate(minLength, maxLength)
#Generate the question
def generate(self, minLength, maxLength):
word1 = randomWord(minLength, maxLength) #Get the first word
self.letter = word1[-1]
regexStart = "^" + self.letter
regexEnd = self.letter + "$"
#Get the other 3 words by searching the word bank using regex
word2 = randomWord(minLength, maxLength, format = regexStart, exclude=[word1])
word3 = randomWord(minLength, maxLength, format = regexEnd, exclude=[word1, word2])
word4 = randomWord(minLength, maxLength, format = regexStart, exclude=[word1, word2, word3])
#Remove the correct letter
self.word1 = word1[:-1]
self.word2 = word2[1:]
self.word3 = word3[:-1]
self.word4 = word4[1:]
#Set the answer
self.correctAnswer = self.letter
#Check if the answer was correct
def verify(self, answer):
if not super().verify(answer):
return False
#Check if the student got the answer the program found
if self.correctAnswer == answer:
return True
if len(answer) != 1:
return False
#Check if the student could still be right
word1 = self.word1 + answer
word2 = answer + self.word2
word3 = self.word3 + answer
word4 = answer + self.word4
if(word1 in words and word2 in words and word3 in words and word4 in words):
self.correctAnswer=answer
self.letter=self.correctAnswer
return True
return False
#Display the question on windows
def displayWindows(self, endFunction, parent, submitNow = True):
tk.Label(parent, text = "In this question, the same letter must fit into both sets of brackets, to complete the word in front of the brackets and begin the word after the brackets.", font=(FONT, 13, "bold", "underline")).pack(padx=5, pady=5)
tk.Label(parent, text=self.word1 + " [ ? ] " + self.word2 + " " + self.word3 + " [ ? ] " + self.word4, font=(FONT, 12)).pack(padx=5, pady=5)
#Answer box
answerBox = tk.Entry(parent, font=(FONT, 12))
answerBox.pack(padx=5, pady=5)
submitButton = None
#Show the result after marking
def showResult():
if answerBox.get() != "":
self.correct = self.verify(answerBox.get())
#Display the result
if(self.correct):
tk.Label(parent, text="Correct!", font=(FONT, 12)).pack(padx=5, pady=5)
else:
tk.Label(parent, text="Incorrect :( The right answer was: '" + self.letter + "'", font=(FONT, 12)).pack(padx=5, pady=5)
letter = self.letter
word1 = self.word1 + letter
word2 = letter + self.word2
word3 = self.word3 + letter
word4 = letter + self.word4
submitButton.destroy()
endFunction(self, [word1, word2, word3, word4], self.type)
#Create the submit button if the question is to be marked immediately after doing it
if submitNow:
submitButton = HoverButton(parent, text = "Submit", command=showResult, **self.submitButtonSettings)
submitButton.pack(padx=5, pady=5)
parent.pack()
return answerBox
#Wrapper display function
def display(self, useragent, *args, **kwargs):
if useragent == ANDROID:
self.displayAndroid()
else:
self.displayWindows(*args, **kwargs)
#View the question and its result after doing it
def view(self, useragent, parent):
tk.Label(parent, text = "In this question, the same letter must fit into both sets of brackets, to complete the word in front of the brackets and begin the word after the brackets.").pack(padx=5, pady=5)
tk.Label(parent, text=self.word1 + " [ ? ] " + self.word2 + " " + self.word3 + " [ ? ] " + self.word4).pack(padx=5, pady=5)
word1 = self.word1 + self.letter
word2 = self.letter + self.word2
word3 = self.word3 + self.letter
word4 = self.letter + self.word4
tk.Label(parent, text="Answer of '" + self.correctAnswer + "' gives the words '" + word1 + "', '" + word2 + "', '" + word3 + "' and '" + word4 + "'").pack(padx=5, pady=5)
parent.pack()
#Question where you have to find a word spread across 2 words in a sentence
class WordInASentenceQuestion(Question):
def __init__(self, minLength = 4, maxLength = 10):
super().__init__()
self.word = ""
self.sentence = ""
self.twoWords = ""
self.options = []
self.type = "Find a word in a sentence question" #Title of the question
self.timeToForm = 1
self.generate(minLength, maxLength)
#Generate the question
def generate(self, minLength, maxLength):
while True:
word = randomWord(minLength, maxLength) #Get a random word
sentence = getRandomSentence(word, maxLength=10) #Get a sentence with that word in
if sentence is None:
continue
sentenceWords = sentence.split() #Get each word in the sentence
#Check if there are any words hidden in the sentence
for i in range(len(sentenceWords) - 1):
combinWord = sentenceWords[i] + sentenceWords[i+1]
if len(combinWord) < minLength + 2:
continue
for j in range(1, len(combinWord)):
for k in range(j, len(combinWord)):
if combinWord[j:k] in allwords and combinWord[j:k] not in sentenceWords[i] and combinWord[j:k] not in sentenceWords[i + 1] and len(combinWord[j:k]) >= minLength and len(combinWord[j:k]) <= maxLength:
#If a word is found, set the question and answer parameters
self.word = combinWord[j:k]
self.sentence = sentence[:1].upper() + sentence[1:] #Capitalise the sentence
self.twoWords = sentenceWords[i] + " " + sentenceWords[i+1]
self.correctAnswer=self.word
#Create options to select from
for l in range(len(sentenceWords) - 1):
self.options.append(sentenceWords[l] + " " + sentenceWords[l+1])
return None
#Check if the answer was correct
def verify(self, answer):
if not super().verify(answer):
return False
#Check if the student got the program answer
if answer == self.correctAnswer:
return True
if answer not in self.options:
return False
index = answer.find(" ")
if index == -1:
return False
#Check if the student was still right
combinWord = answer[:index] + answer[index + 1:]
for i in range(1, len(combinWord)):
for j in range(i, len(combinWord)):
if combinWord[i:j] in words and combinWord[i:j] not in answer[:index] and combinWord not in answer[index + 1:]:
self.word = combinWord[i:j]
self.correctAnswer = answer
return True
return False
#Display the question on windows
def displayWindows(self, endFunction, parent, submitNow = True):
tk.Label(parent, text = "In these sentences, a word is hidden at the end of one word and the beginning of the next word.\nWrite the hidden word in the box below", font=(FONT, 13, "bold", "underline")).pack(padx=5, pady=5)
tk.Label(parent, text=self.sentence, font=(FONT, 12)).pack(padx=5, pady=5)
tk.Label(parent, text="Options:", font=(FONT, 12)).pack(padx=5, pady=5)
optionsFrame = tk.Frame(parent)
for i in range(len(self.options)):
tk.Label(optionsFrame, text=self.options[i], font=(FONT, 12)).grid(row = 0, column = i, padx=5, pady=5)
optionsFrame.pack(padx=5, pady=5)
#Answer box
answerBox = tk.Entry(parent, font=(FONT, 12))
answerBox.pack(padx=5, pady=5)
submitButton = None
#Show the result after marking
def showResult():
if answerBox.get() != "":
self.correct=self.verify(answerBox.get())
#Display the result
if(self.correct):
tk.Label(parent, text="Correct!", font=(FONT, 12)).pack(padx=5, pady=5)
else:
tk.Label(parent, text="Incorrect :( The right answer was: '" + self.correctAnswer + "'", font=(FONT, 12)).pack(padx=5, pady=5)
submitButton.destroy()
endFunction(self, [self.word] + self.sentence.split(), self.type)
#Create the submit button if the question is to be marked immediately after doing it
if submitNow:
submitButton = HoverButton(parent, text = "Submit", command=showResult, **self.submitButtonSettings)
submitButton.pack(padx=5, pady=5)
parent.pack()
return answerBox
#Wrapper display function
def display(self, useragent, *args, **kwargs):
if useragent == ANDROID:
self.displayAndroid()
else:
self.displayWindows(*args, **kwargs)
#View the question and its result after doing it
def view(self, useragent, parent):
tk.Label(parent, text = "In these sentences, a word is hidden at the end of one word and the beginning of the next word.\nWrite the hidden word in the box below", font=(FONT, 13, "bold", "underline")).pack(padx=5, pady=5)
tk.Label(parent, text=self.sentence).pack(padx=5, pady=5)
tk.Label(parent, text="Options:").pack(padx=5, pady=5)
optionsFrame = tk.Frame(parent)
for i in range(len(self.options)):
tk.Label(optionsFrame, text=self.options[i]).grid(row = 0, column = i)
optionsFrame.pack(padx=5, pady=5)
tk.Label(parent, text="Answer is '" + self.correctAnswer + "' to make the word '" + self.word + "'").pack(padx=5, pady=5)
parent.pack()
#Form a word by putting 2 together
class CompoundWordQuestion(Question):
def __init__(self, minLength = 2, maxLength = 10):
super().__init__()
self.word1 = ""
self.word2 = ""
self.answer = ""
self.options = [[], []]
self.type = "Make one word from two question" #Title of the question
self.timeToForm = 3 #The time taken for a question to be generated in seconds
self.generate(minLength, maxLength)
#Generate the question
def generate(self, minLength, maxLength):
w = wordDict
while True:
#Get two words which make one
word1 = randomWord(minLength, maxLength)
word2 = randomWord(minLength, maxLength, exclude=[word1])
if w.get(word1 + word2) != None:
self.word1 = word1
self.word2 = word2
self.options = [[word1], [word2]]
break
if w.get(word2 + word1) != None:
self.word2 = word1
self.word1 = word2
self.options = [[word2], [word1]]
break
#Get the correct answer and options
self.correctAnswer = self.word1 + self.word2
for i in range(2):
for j in range(2):
self.options[i].append(randomWord(minLength, maxLength, exclude=self.options[0] + self.options[1]))
random.shuffle(self.options[0])
random.shuffle(self.options[1])
#Check if the answer was correct
def verify(self, answer):
if not super().verify(answer):
return False
if answer == self.correctAnswer:
return True
#Check if the student was still right
for word1 in self.options[0]:
for word2 in self.options[1]:
if word1 + word2 == answer:
self.word1 = word1
self.word2 = word2
self.correctAnswer=self.answer
return True
return False
#Display the question on windows
def displayWindows(self, endFunction, parent, submitNow = True):
tk.Label(parent, text = "In these questions, find two words, one from each group, that together make one correctly spelt word, without changing the order of the letters.\nThe word from the first group always comes first.\nWrite the word you make in the answer box", font=(FONT, 13, "bold", "underline")).pack(padx=5, pady=5)
tk.Label(parent, text="Options:", font=(FONT, 12)).pack(padx=5, pady=5)
optionsFrame = tk.Frame(parent)
for i in range(2):
for j in range(len(self.options[i])):
tk.Label(optionsFrame, text=self.options[i][j], font=(FONT, 12)).grid(row = j, column = i)
optionsFrame.pack(padx=5, pady=5)
#Answer box
answerBox = tk.Entry(parent, font=(FONT, 12))
answerBox.pack(padx=5, pady=5)
submitButton = None
#Show the result after marking
def showResult():
if answerBox.get() != "":
self.correct=self.verify(answerBox.get())
#Display the result
if(self.correct):
tk.Label(parent, text="Correct!", font=(FONT, 12)).pack(padx=5, pady=5)
else:
tk.Label(parent, text="Incorrect :( The right answer was: '" + self.correctAnswer + "'", font=(FONT, 12)).pack(padx=5, pady=5)
submitButton.destroy()
endFunction(self, [self.answer] + self.options[0] + self.options[1], self.type)
submitButton = HoverButton(parent, text = "Submit", command=showResult, **self.submitButtonSettings)
submitButton.pack(padx=5, pady=5)
parent.pack()
return answerBox
#Wrapper display function
def display(self, useragent, *args, **kwargs):
if useragent == ANDROID:
self.displayAndroid()
else:
self.displayWindows(*args, **kwargs)
#View the question and its result after doing it
def view(self, useragent, parent):
tk.Label(parent, text = "In these questions, find two words, one from each group, that together make one correctly spelt word, without changing the order of the letters.\nThe word from the first group always comes first.\nWrite the word you make in the answer box").pack(padx=5, pady=5)
tk.Label(parent, text="Options:").pack(padx=5, pady=5)
optionsFrame = tk.Frame(parent)
for i in range(2):
for j in range(len(self.options[i])):
tk.Label(optionsFrame, text=self.options[i][j]).grid(row = j, column = i)
optionsFrame.pack(padx=5, pady=5)
tk.Label(parent, text="Answer is '" + self.word1 + " and " + self.word2 + "' to make the word '" + self.correctAnswer + "'").pack(padx=5, pady=5)
parent.pack()
#Simple equation solving question
class AlgebraSubstitutionQuestion(Question):
def __init__(self):
super().__init__()
self.numberMap = {}
self.operations = [" + ", " * ", " / ", " - "]
self.expression = ""
self.answer = ""
self.type = "Letter Calculations"
self.timeToForm = 0
self.generate()
#Generate the question
def generate(self):
while True:
numberMap = {}
letters = ["A", "B", "C", "D", "E"] #Set the letters used to A-E
numbers = []
#Map each letter to a number
for letter in letters:
number = randomNumber(1, 100, exclude=[numbers])
numberMap[number] = letter
numbers.append(number)
#Select the number of operators to have in the calculation
operationNumber = randomNumber(3, 6)
#Set up the operations check to make sure they don't cancel
expression = ""
usedoperations = []
usednumbers = []
used = {" + " : [False, False, False, False, False],
" * " : [False, False, False, False, False],
" - " : [False, False, False, False, False],
" / " : [False, False, False, False, False]}
oppositeop = {" + ": " - ", " - ":" + ", " * ": " / ", " / " : " * "}
complete = True
prevOp = " + "
#Formulate an expression
for i in range(operationNumber):
#Add the number
letterIndex = random.randint(0,4)
currentNumber = numbers[letterIndex]
expression += str(currentNumber)
usednumbers.append(currentNumber)
#Form the operation
currentoperation = self.operations[random.randint(0,3)]
#Update the used dictionary
if prevOp == " * " or prevOp == " / ":
used[prevOp][letterIndex] = True
elif currentoperation == " * " or currentoperation == " / ":
used[" * "][letterIndex] = True
else:
used[prevOp][letterIndex] = True
#Check the operation
if used[oppositeop[currentoperation]][letterIndex]:
complete = False
break
if currentNumber == 1:
if prevOp == " * " or currentoperation == " * " or prevOp == " / ":
complete = False
break
#Add the operation
if i != operationNumber - 1:
expression += currentoperation
usedoperations.append(currentoperation)
#Set the previous operation
prevOp = currentoperation
if not complete:
continue
#Evaluate the expression
result = eval(expression)
if result in numbers:
if result in usednumbers:
correctExpression = True
for number in usednumbers:
if number == result:
if occurences(result, usednumbers) % 2 != 0:
correctExpression = False
break
else:
if occurences(number, usednumbers) % 2 != 0:
break
correctExpression = False
if not correctExpression:
continue
#Make the expression into a string
expression = ""
for i in range(operationNumber - 1):
expression += str(numberMap[usednumbers[i]])
expression += usedoperations[i]
expression += str(numberMap[usednumbers[-1]])
#Make the expression more readable
str.replace(expression, "*", " x ")
str.replace(expression, "/", "÷")
self.expression = expression
self.numberMap = numberMap
self.correctAnswer = numberMap[result]
if len(self.numberMap) == 5:
return 0
#Check if the answer was correct
def verify(self, answer):
if not super().verify(answer):
return False
return answer.upper() == self.correctAnswer
#Display the question on windows
def displayWindows(self, endFunction, parent, submitNow = True):
tk.Label(parent, text = "In these questions, letters stand for numbers.\nWork out the answer to each sum, then find its letter and write it in the box.", font=(FONT, 13, "underline", "bold")).pack(padx=5, pady=5)
n = list(self.numberMap.keys())
nm = self.numberMap
text = ("If " + nm[n[0]] + " = " + str(n[0]) + " and " +
nm[n[1]] + " = " + str(n[1]) + " and " +
nm[n[2]] + " = " + str(n[2]) + " and " +
nm[n[3]] + " = " + str(n[3]) + " and " +
nm[n[4]] + " = " + str(n[4]) + "\nWhat is the answer to\n" + self.expression)
tk.Label(parent, text=text, font=(FONT, 12)).pack(padx=5, pady=5)
#Answer box
answerBox = tk.Entry(parent, font=(FONT, 12))
answerBox.pack(padx=5, pady=5)
submitButton = None
#Show the result after marking
def showResult():
if answerBox.get() != "":
self.correct=self.verify(answerBox.get())
#Display the result
if(self.correct):
tk.Label(parent, text="Correct!", font=(FONT, 12)).pack(padx=5, pady=5)
else:
tk.Label(parent, text="Incorrect :( The right answer was: '" + self.correctAnswer + "'", font=(FONT, 12)).pack(padx=5, pady=5)
submitButton.destroy()
endFunction(self, [], self.type)
#Create the submit button if the question is to be marked immediately after doing it
if submitNow:
submitButton = HoverButton(parent, text = "Submit", command=showResult, **self.submitButtonSettings)
submitButton.pack(padx=5, pady=5)
parent.pack()
return answerBox
#Wrapper display function
def display(self, useragent, *args, **kwargs):
if useragent == ANDROID:
self.displayAndroid()
else:
self.displayWindows(*args, **kwargs)
#View the question and its result after doing it
def view(self, useragent, parent):
tk.Label(parent, text = "In these questions, letters stand for numbers.\nWork out the answer to each sum, then find its letter and write it in the box.").pack(padx=5, pady=5)
n = list(self.numberMap.keys())
nm = self.numberMap
text = ("If " + nm[n[0]] + " = " + str(n[0]) + " and " +
nm[n[1]] + " = " + str(n[1]) + " and " +
nm[n[2]] + " = " + str(n[2]) + " and " +
nm[n[3]] + " = " + str(n[3]) + " and " +
nm[n[4]] + " = " + str(n[4]) + "\nWhat is the answer to\n" + self.expression)
tk.Label(parent, text="Answer is '" + self.correctAnswer + "'").pack(padx=5, pady=5)
parent.pack()
#Mix two words to form another, based on a mix defined in the question
class MixTheWordsInTheSameWayQuestion(Question):
def __init__(self, minLength = 3, maxLength = 3):
super().__init__()
self.word1 = ""
self.word2 = ""
self.word12Combine = ""
self.word3 = ""
self.word4 = ""
self.word34Combine = ""
self.type = "Mix the words in the same way question"
self.timeToForm = 0
self.generate(minLength, maxLength)
#Generate the question
def generate(self, minLength, maxLength):
while True:
#Get two random words
randomWord1 = randomWord(minLength, maxLength)
randomWord2 = randomWord(len(randomWord1), len(randomWord1), exclude=[randomWord1])
#Generate the length of the words to be formed
firstWordLength = random.randint(4, 7)
secondWordLength = random.randint(4, 7)
numberinWord1 = random.randint(1, len(randomWord1) - 1)
#Get a list of the letters to be made into a word
listofletterpositions = [i for i in range(len(randomWord1))]
random.shuffle(listofletterpositions)
#Set up formatting for each word to a default a-z
format1 = ["[a-z]" for i in range(firstWordLength)]
format2 = ["[a-z]" for i in range(secondWordLength)]
format3 = ["[a-z]" for i in range(firstWordLength)]
format4 = ["[a-z]" for i in range(secondWordLength)]
#Correct the format for letters already defined
for i in range(numberinWord1):
format1[listofletterpositions[i]] = randomWord1[listofletterpositions[i]]
format3[listofletterpositions[i]] = randomWord2[listofletterpositions[i]]
for i in range(numberinWord1, len(randomWord1)):
format2[listofletterpositions[i]] = randomWord1[listofletterpositions[i]]
format4[listofletterpositions[i]] = randomWord2[listofletterpositions[i]]
format1 = "".join(format1)
format2 = "".join(format2)
format3 = "".join(format3)
format4 = "".join(format4)
#Generate 4 words following the format
word1 = randomWord(firstWordLength, firstWordLength, format1, wordsList=words)
if word1 != None:
word2 = randomWord(secondWordLength, secondWordLength, format2, [word1], wordsList=words)
if word2 != None:
word3 = randomWord(firstWordLength, firstWordLength, format3, [word1, word2], wordsList=words)
if word3 != None:
word4 = randomWord(secondWordLength, secondWordLength, format4, [word1, word2, word3], wordsList=words)
if word4 != None:
#If 4 words can be found set them as the answers
self.word1 = word1
self.word2 = word2
self.word3 = word3
self.word4 = word4
self.word12Combine = randomWord1
self.word34Combine = randomWord2
self.correctAnswer = randomWord2
return 0
#Check if the answer was correct
def verify(self, answer):
if not super().verify(answer):
return False
if self.correctAnswer == answer:
return True
#Check if the student could still be right
positions = {}
for letter in self.word12Combine:
positions[letter] = []
for pos in findall(self.word1, letter):
positions[letter].append((pos, 1))
for pos in findall(self.word2, letter):
positions[letter].append((pos, 2))
#Recursively find all the possible combinations of the letters over the words
def formulateCombinations(currentLetter, possibleCombinations):
if currentLetter != len(self.word12Combine):
newPossibleCombinations = []
for i in range(len(possibleCombinations)):
for pos in positions[self.word12Combine[currentLetter]]:
if pos not in possibleCombinations[i]:
newPossibleCombinations.append([pos] + possibleCombinations[i])
return formulateCombinations(currentLetter + 1, newPossibleCombinations)
else:
return possibleCombinations
possibleCombinations = formulateCombinations(0, [[]])
possibleWords = []
for combination in possibleCombinations:
possibleWord = ""
for pos in combination:
if pos[1] == 1:
possibleWord += self.word3[pos[0]]
else:
possibleWord += self.word4[pos[0]]
possibleWords.append(possibleWord)
actualWords = []
for word in possibleWords:
if word in words:
actualWords.append(word)
#Check if the word entered is a possible solution
if answer in actualWords:
self.correctAnswer=answer
return True
else:
return False
#Display the question on windows
def displayWindows(self, endFunction, parent, submitNow = True):
tk.Label(parent, text = "In these questions, the three words in the second group should go together in the same way as the three in the first group.", font=(FONT, 13, "underline", "bold")).pack(padx=5, pady=5)
tk.Label(parent, text=self.word1 + " [" + self.word12Combine + "] " + self.word2 + " " + self.word3 + " [ ? ] " + self.word4, font=(FONT, 12)).pack(padx=5, pady=5)
#Answer box
answerBox = tk.Entry(parent, font=(FONT, 12))
answerBox.pack(padx=5, pady=5)
submitButton = None
#Show the result after marking
def showResult():
if answerBox.get() != "":
self.correct=self.verify(answerBox.get())
#Display the result
if(self.correct):
tk.Label(parent, text="Correct!", font=(FONT, 12)).pack(padx=5, pady=5)
self.letter = self.answer
else:
tk.Label(parent, text="Incorrect :( The right answer was: '" + self.correctAnswer + "'", font=(FONT, 12)).pack(padx=5, pady=5)
submitButton.destroy()
endFunction(self, [self.word1, self.word12Combine, self.word2, self.word3, self.word34Combine, self.word4], self.type)
#Create the submit button if the question is to be marked immediately after doing it
if submitNow:
submitButton = HoverButton(parent, text = "Submit", command=showResult, **self.submitButtonSettings)
submitButton.pack(padx=5, pady=5)
parent.pack()
return answerBox
#Wrapper display function
def display(self, useragent, *args, **kwargs):
if useragent == ANDROID:
self.displayAndroid()
else:
self.displayWindows(*args, **kwargs)
#View the question and its result after doing it
def view(self, useragent, parent):
tk.Label(parent, text = "In these questions, the three words in the second group should go together in the same way as the three in the first group.").pack(padx=5, pady=5)
tk.Label(parent, text=self.word1 + " [" + self.word12Combine + "] " + self.word2 + " " + self.word3 + " [ ? ] " + self.word4).pack(padx=5, pady=5)
tk.Label(parent, text="Answer is '" + self.correctAnswer + "'").pack(padx=5, pady=5)
parent.pack()
#Add a 3 letter word into a part word in order to make a sentence make sense
class ThreeLetterWordCompletesSentenceQuestion(Question):
def __init__(self, minLength = 5, maxLength = 10):
super().__init__()
self.sentence = ""
self.wordIn = ""
self.fullWord = ""
self.withRemoved = ""
self.type = "One word in another sentences"
self.options = []
self.timeToForm = 0
self.generate(minLength, maxLength)
#Generate the question
def generate(self, minLength, maxLength):
while True:
fullWord = randomWord(5, maxLength) #Get the full word
halfWord = None
withRemoved = None
#Generate a half word
for i in range(1, len(fullWord) - 3):
if fullWord[i:i+3] in words:
halfWord = fullWord[i:i+3]
withRemoved = fullWord[:i] + fullWord[i+3:]
break
if halfWord is None:
continue
#Get a sentence with the full word in
sentence = getRandomSentence(fullWord, minLength = 4)
if sentence is None:
continue
#Check the word, rather than a derivative, is in the sentence
index = sentence.find(fullWord)
if index == -1:
continue
#Capitalise the first word in the sentence
if index != 0:
sentence = sentence[:1].upper() + sentence[1:]
endWord = sentence.find(" ", index + 1)
#Remove the 3 letters from the word in the sentence
startWord = sentence.rfind(" ", 0, index)
if startWord == -1:
startWord = 0
if endWord == -1:
fullWord = sentence[startWord:]
else:
fullWord = sentence[startWord:endWord]
sentence = sentence[:startWord] + " " + withRemoved.upper() + sentence[startWord + len(fullWord):]
break
#Set the correct answer and options
self.withRemoved = withRemoved
self.fullWord = fullWord
self.wordIn = halfWord
self.correctAnswer = self.wordIn
self.sentence = sentence
self.options = [self.wordIn]
for i in range(4):
self.options.append(randomWord(3,3, exclude = self.options))
random.shuffle(self.options)
#Check if the answer was correct
def verify(self, answer):
if not super().verify(answer):
return False
if answer == self.correctAnswer:
return True
if len(answer) != 3:
return False
if answer not in self.options:
return False
substring = ""
for i in range(1, len(self.withRemoved) - 1):
newWord = self.withRemoved[:i] + answer + self.withRemoved[i:]
if newWord in words:
self.correctAnswer=answer
self.fullWord=newWord
return True
return False
#Display the question on windows
def displayWindows(self, endFunction, parent, submitNow = True):
tk.Label(parent, text = "In these sentences, the word in capitals has had three letters next to each other taken out.\nThese three letters will make one correctly-spelt word without changing their order.\nThe sentence that you make must make sense.", font=(FONT, 13, "underline", "bold")).pack(padx=5, pady=5)
tk.Label(parent, text=self.sentence, font=(FONT, 12)).pack(padx=5, pady=5)
tk.Label(parent, text="Options:", font=(FONT, 12)).pack(padx=5, pady=5)
optionsFrame = tk.Frame(parent)
for i in range(5):
tk.Label(optionsFrame, text=self.options[i], font=(FONT, 12)).grid(row = 0, column = i)
optionsFrame.pack(padx=5, pady=5)
#Answer box
answerBox = tk.Entry(parent, font=(FONT, 12))
answerBox.pack(padx=5, pady=5)
submitButton = None
#Show the result after marking
def showResult():
if answerBox.get() != "":
self.correct=self.verify(answerBox.get())
#Display the result
if(self.correct):
tk.Label(parent, text="Correct!", font=(FONT, 12)).pack(padx=5, pady=5)
self.answer = self.wordIn
else:
tk.Label(parent, text="Incorrect :( The right answer was: '" + self.correctAnswer + "'", font=(FONT, 12)).pack(padx=5, pady=5)
submitButton.destroy()
endFunction(self, [self.fullWord] + self.options, self.type)
#Create the submit button if the question is to be marked immediately after doing it
if submitNow:
submitButton = HoverButton(parent, text = "Submit", command=showResult, **self.submitButtonSettings)
submitButton.pack(padx=5, pady=5)
parent.pack()
return answerBox
#Wrapper display function
def display(self, useragent, *args, **kwargs):
if useragent == ANDROID:
self.displayAndroid()
else:
self.displayWindows(*args, **kwargs)
#View the question and its result after doing it
def view(self, useragent, parent):
tk.Label(parent, text = "In these sentences, the word in capitals has had three letters next to each other taken out.\nThese three letters will make one correctly-spelt word without changing their order.\nThe sentence that you make must make sense.").pack(padx=5, pady=5)