用python编的小学生口算训练题生成器

607 阅读4分钟

小学生口算训练题生成器

这两天学习python,想用python做了一个口算训练题生成器.在网上找了一些类似的代码,但这些代码不能很好的满足我的需求,所以就在别人的代码基础上做了一些修改和优化.

# -*- coding:UTF-8 -*-
'''
这个程序是用来给小学生生产口算训练题的,
自动生成markdown文件,然后用markdown编辑器打印即可,
可通过修改此代码来修改难度和其他运算

'''
__author__ = 'yuanyiba'
__doc__ = """小学生口算生成器"""

from random import randint
from random import random
from datetime import datetime
now = datetime.now()
now_str = datetime.strftime(now,'%y-%m-%d-%H-%M-%S')
#print(now_str)

sym = [' + ', ' - ']

# 当前文件夹下创建口算题目文件口算训练+日期.md
fobj = open('口算训练'+ now_str+'.md', 'w')

# 加减法:... +/- ... +/- ...
def kousuan_str(summin=1, summax=20):
    '''
    summin,summax:允许结果的最小值和最大值
    最小值和最大值的差值要不小于2

    '''
    sym1 = sym[randint(0, 1)]
    sym2 = sym[randint(0, 1)]

    if sym1 == ' + ' and sym2 == ' + ':
        结果 = randint(summin + 2, summax)
        first = randint(summin, 结果 - 2)
        中间值 = 结果 - first
        second = round(中间值*random())
        third = 中间值 - second
    elif sym1 == ' + ' and sym2 == ' - ':
        中间值1 = randint(summin,summax)
        中间值2 = randint(summin,summax)
        if 中间值1>中间值2:
            结果 = 中间值2
            first = round(中间值1*random())
            second = 中间值1 - first
            third = 中间值1 - 中间值2
        else:
            结果 = 中间值1
            first = round(中间值2*random())
            second = 中间值2 - first
            third = 中间值2 - 中间值1
    elif sym1 == ' - ' and sym2 == ' + ':
        中间值1 = randint(summin,summax)
        中间值2 = randint(summin,summax)
        if 中间值1>中间值2:
            third = 中间值2
            结果 = 中间值1
        else:
            third = 中间值1
            结果 = 中间值2
        second = randint(summin,summax)
        while 结果-third+second>summax:
            second = randint(summin,summax)
        first = 结果 - third + second
    elif sym1 == ' - ' and sym2 == ' - ':
        结果 = randint(summin, summax)
        first = randint(结果,summax)
        中间值 = first - 结果
        while 中间值<=1:
            结果 = randint(summin, summax)
            first = randint(结果,summax)
            中间值 = first - 结果
        second = round(中间值*random())
        third = 中间值 - second
        

    arithmetic = str(first).rjust(2) + sym1 + str(second).rjust(2) + sym2 + str(third).rjust(2)+ ' = '
 #   print(arithmetic)
    return arithmetic


# 生成一套题
def oneday_homework():   
    for  i in range(25):
        plus1 = kousuan_str()
        plus2 = kousuan_str()
        plus3 = kousuan_str()
        plus4 = kousuan_str()
        line = [plus1,plus2,plus3,plus4]
        lines = ( ' | ').join(line)
 #       print (lines)
        fobj.writelines(' | '+ lines + ' | \n')

    


# 一张A4纸上打印
fobj.writelines(r'####    20以内加减法口算训练,用时__分钟,对__题(共100题),日期_____ '+ '\n')
fobj.writelines(r'| 1-25题 | 26-50题 | 51-75题 | 76-100题 |'+ '  \n')
fobj.writelines(r'| ----- | ----- | ----- | ----- |'+ '  \n')
oneday_homework()
print ('生成成功!请用markdown编辑器打开,打印.')
fobj.close()

生成的文件内容:

20以内加减法口算训练,用时__分钟,对__题(共100题),日期_____

1-25题26-50题51-75题76-100题
9 - 3 + 2 =1 + 4 + 12 =12 - 5 - 1 =18 - 1 - 5 =
11 - 10 + 12 =11 + 7 - 14 =19 - 13 - 1 =18 - 12 + 9 =
1 + 1 + 11 =12 - 6 - 2 =2 + 15 - 11 =15 + 4 - 16 =
4 + 15 - 1 =14 - 4 + 6 =3 + 0 - 1 =2 + 5 - 5 =
13 - 10 + 12 =6 + 7 + 1 =4 + 12 - 6 =13 + 0 - 9 =
16 - 5 - 5 =5 + 1 + 1 =7 + 2 + 2 =19 - 6 - 0 =
3 + 17 - 16 =5 - 2 + 9 =1 + 0 + 3 =12 + 3 - 1 =
10 - 6 + 13 =13 + 1 - 3 =2 + 2 + 1 =4 + 15 - 11 =
13 - 9 + 11 =15 - 15 + 10 =17 - 0 - 5 =20 - 13 + 6 =
17 - 0 - 2 =15 - 1 + 3 =10 - 8 - 1 =3 + 4 + 9 =
11 - 1 + 7 =15 - 3 - 1 =12 - 5 + 1 =4 - 3 + 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