-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid1.py
110 lines (94 loc) · 3.7 KB
/
grid1.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
import kivy
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
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
def control(self, instance):
if instance.text == "exit":
App.get_running_app().stop()
return
if instance.text == "start":
self.qNum = 0
self.startTime = datetime.now()
self.nextQuestion()
return
if instance.text == "end":
self.summary()
return
print instance.text, "pressed"
def summary(self):
self.currentQuestion = ''
total = (datetime.now() - self.startTime).total_seconds() / self.qNum
pcnt = 100. * self.numCorrect / self.qNum
self.question.text = "Finished: %d / %d (%.1f%%) avg %.1f seconds each" \
% (self.numCorrect, self.qNum, pcnt, total)
self.numCorrect = 0
self.qNum = 0
def nextQuestion(self, other=None):
if other:
print 'other %s' % other
if self.qNum == self.numTot:
# summary
self.summary()
return
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 = '%3d / %d: %s' % (self.qNum, self.numTot, self.currentQuestion)
def number(self, instance):
value = int(instance.text)
if not self.currentQuestion:
return
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.nextQuestion, 5)
def build(self):
# Overall layout
layout = GridLayout(cols=1, row_force_default=True, row_default_height=40)
# 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))
row2 = BoxLayout(orientation='horizontal')
layout.add_widget(row2)
# Question label
self.question = Label(text="Question", markup=True)
row2.add_widget(self.question)
## # answer label
## self.answer = Label(text="answer", font_size='20sp', markup=True)
## row2.add_widget(self.answer)
# number rows
for loop in range(1,101):
if loop % 10 == 1:
row = BoxLayout(orientation='horizontal')
layout.add_widget(row)
num = Button(text='%d' % loop, on_press=self.number, background_color=[1,0,0,1])
row.add_widget(num)
return layout
MultiplicationPracticeApp().run()