I've written a small app using the kivy framework, which aims at reviewing your times tables. My major concern is about making this app easier to improve in the future. That is, how to organize the code to make it more clearer (by adding a User
class that would store the data associated to the user, for instance). Since the code pretty straightforward, I've not added documentation to it.
The Python code:
# -*- coding: utf-8 -*-
from numpy.random import randint
import kivy
kivy.require('1.9.1-dev')
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.properties import StringProperty
Window.size = (200, 100)
class MyWidget(Widget):
help_message = StringProperty()
question = StringProperty()
feedback = StringProperty()
def __init__(self):
super(MyWidget, self).__init__()
self.help_message = "Your answer"
self.question = ""
self.answer = 0
self.points = 0
self.attempts = 1e-10
self.feedback = ""
self.set_question()
def set_question(self):
a, b = randint(1, 11, 2)
self.question = "{} x {} =".format(a, b)
self.answer = a * b
def check_answer(self, user_input):
try:
if int(user_input) == self.answer:
self.points += 1
self.feedback = "right"
else:
self.feedback = "{} {}".format(self.question, self.answer)
self.set_question()
self.attempts += 1
except:
self.feedback = "invalid answer"
def set_score(self):
score = self.points / self.attempts * 100
self.feedback = "{:3.0f}% of right answers".format(score)
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
The .kv one:
<MyWidget>:
canvas:
Color:
rgba: .93, .93, .93, 1.
Rectangle:
pos: self.pos
size: self.size
TextInput:
size: 130, 30
pos: 70, 30
text: root.help_message
background: 1., 1., 1., 1.
font_size: 14
multiline: False
on_text_validate: root.check_answer(self.text); self.text = ""
Label:
size: 200, 30
pos: 0, 60
text: root.feedback
color: .0, .0, .0, 1.
font_size: 14
Label:
size: 70, 30
pos: 0, 30
text: root.question
color: .0, .0, .0, 1.
font_size: 14
Button:
size: 200, 30
pos: 0, 0
text: "Stop"
color: .0, .0, .0, 1.
font_size: 14
on_release: root.set_score()
[EDIT] Mentioned code reviewer here.