954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Training passing ten in addition and substraction

0
By Tony Veijalainen on Jul 12th, 2011 4:39 pm

Python helps in real life situation to automate things. This program helps you to prep up your kids ability to go over/under ten, which is essential not only by itself, but also when calculating with bigger numbers.

Program is not any special, but shows how useful Python can be in real life situations.

from __future__ import print_function
import random

def exercises(count, perline=3, width=80):
    print((width*'=').join('\n\n'))
    w = width / perline
    for i in range(count):
        if not i % perline:
            print('\n\n')
        a = random.randint(1,9)
        c = random.randint(10,10+a-1)
        b = c-a
        case = random.randint(0,5)
        if case == 0:
            ex = '%2i - %2i = ______'.ljust(w) % (c, a)
        elif case == 1:
            ex = '%2i + %2i = ______'.ljust(w) % (a, b)
        elif case == 2:
            ex = '%2i + ______ = %2i'.ljust(w) % (a, c)
            
        elif case == 3:
            ex = '%2i - ______ = %2i'.ljust(w) % (c, b)
        elif case == 4:
            ex = '______ - %2i = %2i'.ljust(w) % (a, b)           
        else:
            ex = '______ + %2i = %2i'.ljust(w) % (a, c)
        print(ex, end= '')
    print()    
    print((width*'=').join('\n\n'))
          
exercises(int(raw_input('How many exercises? ')))

New day, new exercises. The program is easy to change for practising multiplication/division in upto 10*10 region. Same exercise might repeat, but it is too much trouble to check for that. If you must check, you should generate all possible exercises and take random.choice from that, removing each used one as you print.

from __future__ import print_function
import random

def exercises(count, perline=3, width=80):
    print((width * '=').join('\n\n'))
    w = width / perline
    for i in range(count):
        if not i % perline:
            print('\n\n')
        a = random.randint(1,9)
        b = random.randint(1,9)
        c = a * b
        case = random.randint(0,5)

        if case == 0:
            ex = '%2i / %2i = ______'.ljust(w) % (c, a)
        elif case == 1:
            ex = '%2i * %2i = ______'.ljust(w) % (a, b)
        elif case == 2:
            ex = '%2i * ______ = %2i'.ljust(w) % (a, c)
            
        elif case == 3:
            ex = '%2i / ______ = %2i'.ljust(w) % (c, b)
        elif case == 4:
            ex = '______  / %2i = %2i'.ljust(w) % (a, b)           
        else:
            ex = '______  * %2i = %2i'.ljust(w) % (a, c)
        print(ex, end= '')
    print()    
    print((width * '=').join('\n\n'))
          
exercises(int(raw_input('How many exercises? ')))
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: