假期的时候给孩子教了下算数的东东,为了检验学习效果,也会出一些题来验证下学习效果。

每次出题都会花一些时间,琢磨怎么能够用到知识点,怎么又不算超纲。下午的时候想,干脆写个程序来出题吧,

# -*- coding=utf-8 -*-


import sys
import random
import time


sys.setrecursionlimit(100)




def randint():
    return random.randint(0,20)


def get_add_result(x,y):
    if x + y <= 20 :
        print ('%2d + %2d = ? ' % (x, y))
        time.sleep(3)
        print ('答案是:')
        return ('%2d + %2d = %2d ' %(x,y,x+y))
    else:
        return get_add_result(randint(),randint())


def get_sub_result(x,y):
    if x-y > 0:
        print ('%2d - %2d = ? ' % (x, y))
        time.sleep(3)
        print ('答案是:')
        return ('%2d - %2d = %2d ' %(x,y,x-y))
    else:
        return get_sub_result(randint(),randint())
def get_calc_type():
    calc_type = random.randint(0,1)
    if calc_type == 0:
        return get_add_result(randint(),randint())
    else:
        return get_sub_result(randint(), randint())


def get_result():
    for i in range(20):
        print ('请看第%s题:' %(i+1))


        print (get_calc_type())
        time.sleep(3)


if __name__ == '__main__':
    get_result()

运行程序会有几秒的停顿,然后输出答案。

明天看看小朋友的反应和建议,当然对我来说也有用,把数的范围放大,我就可以自己玩玩了。