Training passing ten in addition and substraction

Updated TrustyTony 0 Tallied Votes 268 Views Share

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? ')))
TrustyTony 888 pyMod Team Colleague Featured Poster

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? ')))
TrustyTony 888 pyMod Team Colleague Featured Poster

The last post was not tested on Python3, so here is the working version for both:

from __future__ import print_function
try:
    input = raw_input
except:
    pass

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(input('How many exercises? ')))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.