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
|
import gtk, math
import random, locale
from sugar.activity import activity
class HopaRoundActivity(activity.Activity):
def __init__(self, handle):
activity.Activity.__init__(self, handle)
print "INFO: activity running"
locale.setlocale(locale.LC_ALL,"")
# make the toolbox
toolbox = activity.ActivityToolbox(self)
self.set_toolbox(toolbox)
toolbox.show()
#initialize variables
print "INFO: initialize variables"
self.decades = (10,100,1000,10000,100000)
self.rounded={0:0,1:1,2:2,3:3,4:4,9:0,10:0,11:0,99:0,100:0,101:0,999:0,1000:0,1001:0,9999:0,10000:0,10001:0,99999:0,100000:0,100001:0}
self.lower_bound = 1
self.upper_bound = 500000
self.correct = 0
self.incorrect = 0
self.dec_answer = 0
random.seed()
self.correct_answer = gtk.gdk.pixbuf_new_from_file("hoppy-right.svg")
self.incorrect_answer = gtk.gdk.pixbuf_new_from_file("hoppy-wrong.svg")
self.image_answer = gtk.Image()
#create view ports
print "INFO: create view ports"
self.main_view = gtk.HBox()
self.panels = gtk.VBox()
self.panes = gtk.HBox(True)
self.left_view = gtk.VBox()
self.mid_view = gtk.VBox()
self.right_view = gtk.VBox()
self.instructions = gtk.Label()
self.instructions.set_justify(gtk.JUSTIFY_LEFT)
self.instructions.set_markup("\n\nLet's play...")
self.type_answer_instructions = gtk.Label("Type your answer below")
self.mult_instructions = gtk.Label("Choose one of the answers below")
self.answer = gtk.Entry()
self.answer.set_text("?")
self.answer.connect("activate", self.check_answer, 0)
self.play_btn = gtk.Button("Play")
self.play_btn.connect("clicked", self.play)
self.check_answer_btn = gtk.Button("Check Answer")
self.check_answer_btn.connect("clicked", self.check_answer, 0)
self.mult_1 = gtk.Button()
self.mult_1.connect("clicked", self.check_answer, 1)
self.mult_2 = gtk.Button()
self.mult_2.connect("clicked", self.check_answer, 2)
self.mult_3 = gtk.Button()
self.mult_3.connect("clicked", self.check_answer, 3)
self.mult_4 = gtk.Button()
self.mult_4.connect("clicked", self.check_answer, 4)
self.output = gtk.Label()
self.output.set_justify(gtk.JUSTIFY_LEFT)
#show the widgets
print "INFO: show widgets"
self.main_view.pack_start(self.panels, False, False, 10)
self.main_view.pack_start(self.right_view, False, False, 10)
self.panels.pack_start(self.instructions, False, False, 10)
self.panels.pack_start(self.play_btn, False, False, 10)
self.panels.pack_start(self.panes, False, False, 10)
self.panes.pack_start(self.left_view, False, False, 10)
self.panes.pack_start(self.mid_view, False, False, 10)
self.left_view.pack_start(self.type_answer_instructions, False, False, 10)
self.left_view.pack_start(self.answer, False, False, 10)
self.left_view.pack_start(self.check_answer_btn, False, False, 10)
self.mid_view.pack_start(self.mult_instructions, False, False, 10)
self.mid_view.pack_start(self.mult_1, False, False, 10)
self.mid_view.pack_start(self.mult_2, False, False, 10)
self.mid_view.pack_start(self.mult_3, False, False, 10)
self.mid_view.pack_start(self.mult_4, False, False, 10)
self.right_view.pack_start(self.output, False, False, 10)
self.right_view.pack_start(self.image_answer, False, False, 10)
self.set_canvas(self.main_view)
self.show_all()
self.play_btn.grab_focus()
def play(self, widget):
print "INFO: play clicked"
self.dec_answer = random.choice(self.decades)
self.gen_random(self)
self.guesses = 0
self.answer.set_text("?")
message = "\nRound the number <b>"
message += locale.format("%d", self.rounded[0], True)
message += "</b> to the nearest <b>"
message += locale.format("%d", self.dec_answer, True)
message += "</b>:\n"
self.instructions.set_markup(message)
self.mult_1.set_label(locale.format("%d", self.rounded[1], True))
self.mult_2.set_label(locale.format("%d", self.rounded[2], True))
self.mult_3.set_label(locale.format("%d", self.rounded[3], True))
self.mult_4.set_label(locale.format("%d", self.rounded[4], True))
message = "Correct:\t\t" + str(self.correct)
message += "\nIncorrect:\t" + str(self.incorrect)
self.image_answer.clear()
self.output.set_markup(message)
#self.show_random_numbers(self)
self.answer.grab_focus()
def check_answer(self, widget, response=None):
print "INFO: check answer clicked"
print "INFO: response = '" + str(response) + "'"
try:
if response == 0:
print "INFO: entry = '" + str(self.answer.get_text()) + "'"
response = int(self.answer.get_text())
elif response == 1: response = self.rounded[response]
elif response == 2: response = self.rounded[response]
elif response == 3: response = self.rounded[response]
elif response == 4: response = self.rounded[response]
if response == self.rounded[self.dec_answer]:
self.correct += 1
message = "Correct:\t\t" + str(self.correct)
message += "\nIncorrect:\t" + str(self.incorrect)
message += "\n\n<b>That's correct!\n\n\n\n</b>"
self.output.set_markup(message)
self.image_answer.set_from_pixbuf(self.correct_answer)
self.instructions.set_markup("\n\nLet's play...")
self.answer.set_text("?")
self.play_btn.grab_focus()
else:
self.incorrect += 1
message = "Correct:\t\t" + str(self.correct)
message += "\nIncorrect:\t" + str(self.incorrect)
message += "<span foreground=\"red\">\n\n<b>Sorry, the correct answer was\n"
message += locale.format('%d', self.rounded[self.dec_answer], True)
message += "\n\nYou answered\n" + str(response)
message += "</b></span>"
self.output.set_markup(message)
self.image_answer.set_from_pixbuf(self.incorrect_answer)
self.answer.set_text("?")
self.play_btn.grab_focus()
except:
message = "<span foreground=\"red\"><b>Please enter a number only.</b></span>"
self.output.set_markup(message)
self.answer.grab_focus()
def gen_random(self, widget):
print "INFO: generating random number"
#generate a random number and round it to the different decades
self.rounded[0] = random.randint(self.lower_bound,self.upper_bound)
for item in self.decades:
self.rounded[item-1] = int(math.floor(self.rounded[0]/(item*1.0)) * item)
self.rounded[item] = int(round(self.rounded[0]/(item*1.0), 0) * item)
self.rounded[item+1] = int(math.ceil(self.rounded[0]/(item*1.0)) * item)
#create a list of decades that are not the right answer and choose one
self.dec_temp_list = [10, 100, 1000, 10000, 100000]
del self.dec_temp_list[self.dec_temp_list.index(self.dec_answer)]
self.dec_temp = random.choice(self.dec_temp_list)
#generate the multiple choice answer set
self.mult = range(1,5)
self.mult_temp = random.choice(self.mult)
self.rounded[self.mult_temp] = self.rounded[self.dec_answer - 1]
del self.mult[self.mult.index(self.mult_temp)]
self.mult_temp = random.choice(self.mult)
self.rounded[self.mult_temp] = self.rounded[self.dec_answer + 1]
del self.mult[self.mult.index(self.mult_temp)]
self.mult_temp = random.choice(self.mult)
self.rounded[self.mult_temp] = self.rounded[self.dec_temp - 1]
del self.mult[self.mult.index(self.mult_temp)]
self.mult_temp = random.choice(self.mult)
self.rounded[self.mult_temp] = self.rounded[self.dec_temp + 1]
def show_random_numbers(self, widget):
print "INFO: showing random numbers"
message = "Lower Bound: \t\t\t<b>" + str(self.lower_bound) + "</b>"
message += "\n\nUpper Bound: \t\t\t<b>" + str(self.upper_bound) + "</b>"
message += "\n\nNumber Generated: \t<b>" + str(self.rounded[0]) + "</b>"
message += "\n"
for item in self.rounded:
#1message += "\n\nRounded to 10<sup>" + str(int(math.log10(item))) + "</sup>: \t\t<b>" + str(self.rounded[item]) + "</b>"
#2message += str(item-1) + ": \t" + str(self.rounded[item-1]) + "\n"
#2message += str(item) + ": \t" + str(self.rounded[item]) + "\n"
#2message += str(item+1) + ": \t" + str(self.rounded[item+1]) + "\n"
message += str(item) + ":\t" + str(self.rounded[item]) + "\n"
self.output.set_markup(message)
|