[Thiago Cafe] Programming is fun!

Math - practicing sum

Created by thiago on 2017-05-01 21:36:12


My daughters were using a website called xtramath to practice sums. It's a very nice website were lots of different sums are shuffled and asked - with time to answer, which works nice to memorize, but not to calculate.

Thinking about that, I decided to write an alternative to it without timeout and quick to write.

What should it do ?

My plans are:

  1. Ask which number should I use as operand
  2. Get numbers from 1 to 10 and shuffle them
  3. For each shuffled number, ask the result of the sum


I'm using the shuffling algorithm from last post (Bingo - shuffling the balls)

That was easy, let's get going.

Read a number from stdin

First, let's read a number from stdin. I don't want to ask forever, therefore we need an exit condition.

def get_number(s, exit_cond):
    while( True ):
    string = raw_input(s)
    if exit_cond(string):
        return None
    return int(string)
    print 'Not a number. Try again'

Now we can simply read this number using a lambda as exit condition

exit_cond = lambda x: x in {'q', 'quit', 'leave', 'exit'}
number = get_number('Choose the number you want to practice sum: ', exit_cond)

So far, so good. Now for each number from 1 to 10, I need to ask the result of the question

Asking the question

I do not want to ask a question and give a single try. I am going to ask three times before consider it wrong. IMPORTANT: range(1, 4) in python is goes from 1 to 3

def ask_question(m, n, exit_cond):
    for i in range(1, 4):
    result = get_number(str(m) + ' + ' + str(n) + ' = ', exit_cond)
    if result == None:
        return -1

    if result == (m+n):
        print 'Correct !'
        return 1
        print 'Wrong. try again!'
    return 0

Main function

And the main function was basically the following

operands = shuffler.shuffle_numbers(range(1, 11))

count = 0
for operand in operands:
    ret = ask_question(number, operand, exit_cond)
    if ret == -1:
    count += ret
    print ''

Colours !

The first version was monochromatic and boring. I decided to make it colourful.

With a quick search in stack overflow, I found some patterns to write decorated text to console:

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

Now I just have to add some colourful text

print bcolors.HEADER + bcolors.UNDERLINE + 'Welcome to the Thiago\'s super sum practice!' + bcolors.ENDC
# ....
total = len(operands)
if count < (int(total * .7)):
    print bcolors.WARNING + 'Try harder. You got only', count, 'correct answers' + bcolors.ENDC
    print bcolors.OKGREEN + bcolors.UNDERLINE + 'Congratulations. You got', count, 'correct answers!' + bcolors.ENDC


Now my girls can have lots of fun with some math calculations.

Any comments or questions, compliments ?

Reach me on twitter - @thiedri

Tell me your opinion!

Reach me on Twitter - @thiedri