-
Notifications
You must be signed in to change notification settings - Fork 0
/
numpad.py
180 lines (160 loc) · 5.84 KB
/
numpad.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
import kivy
import re
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.app import App
from random import randrange
from datetime import datetime
digitRE = re.compile(r'^\d$')
class MultiplicationPracticeApp(App):
def __init__(self):
super(MultiplicationPracticeApp, self).__init__()
self.currentQuestion = ''
self.qNum = 0
self.numTot = 10
self.numbers = range(3,10)
self.numCorrect = 0
self.current = '0'
self.maxNumDigits = 3
self.askingQuestion = False
self.totSeconds = 0.
self.working = True
def control(self, instance):
if instance.text == "exit":
App.get_running_app().stop()
return
if instance.text == "start":
self.qNum = 0
self.working = True
self.startTime = datetime.now()
self.almostNextQuestion()
return
if instance.text == "end":
self.working = False
if not self.askingQuestion:
self.summary()
return
def summary(self):
self.currentQuestion = ''
if not self.qNum:
self.question.text = "No questions asked"
return;
if self.qNum:
total = self.totSeconds / self.qNum
pcnt = 100. * self.numCorrect / self.qNum
else:
total = 0
pcnt = 0
self.question.text = "Finished: %d / %d (%.1f%%) avg %.1f seconds each" \
% (self.numCorrect, self.qNum, pcnt, total)
self.numCorrect = 0
self.qNum = 0
def almostNextQuestion(self, other=None):
if self.qNum == self.numTot or not self.working:
# summary
self.summary()
return
self.question.text = "Next question:"
Clock.schedule_once(self.nextQuestion, 1)
def nextQuestion(self, other=None):
if not self.working:
self.summary()
return
self.current = '0'
self.updateDisplay()
first = self.numbers[randrange(len(self.numbers))]
second = randrange(2, 10)
if randrange(2):
first, second = second, first
self.currentQuestion = '%d x %d' %(first, second)
self.currentAnswer = first * second
self.qNum += 1
self.question.text = 'Question %3d of %d: %s' % (self.qNum, self.numTot, self.currentQuestion)
self.askingQuestion = True
self.startTime = datetime.now()
def number(self, instance):
if not self.askingQuestion:
return
value = instance.text
if value == 'Done':
value = int(self.current)
self.fullEntry(value)
self.askingQuestion = False
return
elif digitRE.search(value):
if len(self.current) >= self.maxNumDigits:
# don't bother
return
if value == '0' and self.current == '0':
# it's a zero and we hit zero. don't bother
return
if self.current == '0':
self.current = value
else:
self.current += value
elif value == "<":
# removing digits
if self.current == "0":
# nothing to do
return
if len(self.current) == 1:
# it's a single non-zero digit
self.current = '0'
else:
self.current = self.current[:-1]
self.updateDisplay()
def updateDisplay(self):
current = self.current
if current == '0':
current = ''
self.answer.text = current
def fullEntry(self, value):
if not self.currentQuestion:
return
self.totSeconds += (datetime.now() - self.startTime).total_seconds()
if value == self.currentAnswer:
# correct()
text = '[color=00ff00]Correct! %s = %d[/color]' % \
(self.currentQuestion, value)
self.numCorrect += 1
else:
# wrong
text = '[color=ff0000]Wrong! %s = %d, not %d[/color]' % \
(self.currentQuestion, self.currentAnswer, value)
self.question.text = text
Clock.schedule_once(self.almostNextQuestion, 4)
def build(self):
# Overall layout
layout = GridLayout(cols=1)
# Control row
row1 = BoxLayout(orientation='horizontal')
layout.add_widget(row1)
row1.add_widget(Button(text="start", on_press=self.control))
row1.add_widget(Button(text="end" , on_press=self.control))
row1.add_widget(Button(text="exit" , on_press=self.control))
# text
row2 = BoxLayout(orientation='horizontal')
layout.add_widget(row2)
# Question label
self.question = Label(text="", markup=True)
row2.add_widget(self.question)
# answer label
self.answer = Label(text="", markup=True)
row2.add_widget(self.answer)
# number rows
lines = []
for outer in range(3):
lines.append(['%s' % (3 * outer + x + 1) for x in range(3)])
lines.reverse()
lines.append(["<","0","Done"])
for line in lines:
row = BoxLayout(orientation='horizontal')
layout.add_widget(row)
for value in line:
num = Button(text=value, on_press=self.number, background_color=[1,0,0,1])
row.add_widget(num)
return layout
MultiplicationPracticeApp().run()