龙空技术网

只要5行核心代码,Python给孩子出数学练习题,做到手软

PythonOffice 171

前言:

现时朋友们对“python数学代码”大致比较珍视,你们都需要学习一些“python数学代码”的相关文章。那么小编也在网上汇集了一些对于“python数学代码””的相关内容,希望姐妹们能喜欢,各位老铁们快快来学习一下吧!

Python除了用于工作,也能用于家庭事务,比如给孩子出数学练手题。数学基本四则运算,需要多练习,才能让技能长在大脑里。手写出题,看似简单,其实蛮纠结的。因为需要调用大脑不停地选择数字,选择太多,实在耗费精力。用Python给孩子出题,核心代码就5行,想出多少出多少,而且每次的题都是不一样的(如下程序)。

import randomfor i in range(5):    num1 = random.randint(1,9)    num2 = random.randint(1,9)    print(f"{num1} + {num2} = ")>>7 + 8 = 4 + 6 = 1 + 9 = 4 + 5 = 1 + 2 = 

主要用到了random模块,可以生成随机数。这样每次运行程序,都是一套新题,孩子永远也做不完,哈哈。千万别滥用此技能虐待小朋友哦。现在以基本四则运算的“加减乘除”为例来准备程序吧。下面先看加法和乘法题生成程序。因为加法和乘法对于前后数字没什么要求,只需要将两个数字中的运算符号改一下就行了,所以一个程序搞定。

如下先导入random模块,用于生成随机数。然后建一个空列表data,用于储存题目。group是一个中转列表,用于将题目按四个分成一组存放。为什么要四个呢?因为在将题目填入word文档的时候,四个一行比较合适。然后使用while语句控制题目的个数。测试了一下,80个题刚好占据word文档的1页,所以令len(data) < 20,即只要列表data中题目的行数不超过20(即80个题),就持续生成新的题目,等于20的时候就停止循环。然后定义两个随机整数,都从0~9中选取。选好后,则将设定好的格式,比如7 + 8 =放入中转列表group。当中转列表group放满4道题的时候len(group) == 4,就将group列表放入data列表中去。然后令group=[]将group清空,以便下一次中转。

数据瞬间生成,然后看一下data的内容,真是我们想要的。如果要生成乘法题,只需要将+改成×即可。

#加法、乘法题import randomdata = [] #储存题目的列表group = [] #中转列表while len(data) < 20:  #题目个数,20行×4列共80个题,刚好放入word文档中的1页    num1 = random.randint(0,9) #随机整数1,从0~9中选取    num2 = random.randint(0,9) #随机整数2,从0~9中选取       group.append(f"{num1} + {num2} = ") #数据放入中转列表    if len(group) == 4: #按四道题分一组        data.append(group) #数据放入总列表        group = []#清空中转列表data[['7 + 8 = ', '0 + 2 = ', '1 + 5 = ', '0 + 5 = '], ['4 + 5 = ', '7 + 6 = ', '7 + 3 = ', '4 + 6 = '], ['9 + 5 = ', '8 + 6 = ', '0 + 9 = ', '9 + 9 = '], ['7 + 7 = ', '2 + 9 = ', '1 + 4 = ', '7 + 1 = '], ['2 + 4 = ', '4 + 1 = ', '0 + 3 = ', '7 + 6 = '], ['2 + 0 = ', '8 + 7 = ', '8 + 9 = ', '4 + 9 = '], ['6 + 3 = ', '4 + 1 = ', '9 + 3 = ', '8 + 6 = '], ['1 + 1 = ', '3 + 0 = ', '5 + 9 = ', '2 + 1 = '], ['6 + 2 = ', '4 + 9 = ', '2 + 1 = ', '9 + 0 = '], ['9 + 2 = ', '3 + 6 = ', '9 + 2 = ', '7 + 3 = '], ['6 + 1 = ', '6 + 3 = ', '1 + 7 = ', '6 + 4 = '], ['4 + 5 = ', '6 + 9 = ', '2 + 8 = ', '6 + 0 = '], ['5 + 6 = ', '4 + 5 = ', '7 + 0 = ', '6 + 9 = '], ['3 + 0 = ', '7 + 8 = ', '9 + 4 = ', '3 + 8 = '], ['9 + 4 = ', '1 + 6 = ', '7 + 6 = ', '7 + 2 = '], ['5 + 8 = ', '4 + 9 = ', '1 + 0 = ', '7 + 2 = '], ['3 + 6 = ', '5 + 6 = ', '2 + 3 = ', '3 + 8 = '], ['7 + 2 = ', '4 + 4 = ', '9 + 1 = ', '0 + 3 = '], ['4 + 6 = ', '5 + 5 = ', '2 + 6 = ', '2 + 0 = '], ['6 + 5 = ', '9 + 2 = ', '3 + 5 = ', '5 + 4 = ']]

下面准备减法题。对于小孩子来说,还没有负数概念,所以在出题的时候得保证被减数要大于等于减数(即减号前的数要大于等于减号后的数)。因此,相对加法题程序,增加了一个if判断语句,使num1 >= num2。其余内容都一样。

#减法题import randomdata = [] #储存题目的列表group = [] #中转列表while len(data) < 20:    num1 = random.randint(1,9)    num2 = random.randint(1,9)    if num1 >= num2:        group.append(f"{num1} - {num2} = ") #数据放入中转列表        if len(group) == 4: #按四道题分一组            data.append(group) #数据放入总列表            group = []#清空中转列表data[['9 - 9 = ', '7 - 4 = ', '8 - 2 = ', '7 - 1 = '], ['2 - 2 = ', '6 - 5 = ', '6 - 4 = ', '6 - 4 = '], ['8 - 2 = ', '4 - 1 = ', '9 - 4 = ', '4 - 2 = '], ['6 - 5 = ', '7 - 5 = ', '8 - 4 = ', '9 - 4 = '], ['9 - 4 = ', '3 - 3 = ', '3 - 1 = ', '4 - 2 = '], ['6 - 5 = ', '9 - 1 = ', '7 - 6 = ', '6 - 1 = '], ['9 - 7 = ', '3 - 3 = ', '9 - 3 = ', '8 - 8 = '], ['5 - 3 = ', '9 - 8 = ', '7 - 5 = ', '4 - 4 = '], ['9 - 6 = ', '8 - 5 = ', '9 - 4 = ', '7 - 1 = '], ['4 - 4 = ', '3 - 1 = ', '8 - 7 = ', '9 - 7 = '], ['5 - 4 = ', '8 - 4 = ', '9 - 4 = ', '7 - 7 = '], ['4 - 3 = ', '8 - 1 = ', '3 - 3 = ', '6 - 3 = '], ['9 - 2 = ', '8 - 3 = ', '9 - 8 = ', '9 - 5 = '], ['5 - 2 = ', '8 - 5 = ', '7 - 2 = ', '9 - 8 = '], ['5 - 1 = ', '6 - 1 = ', '9 - 1 = ', '5 - 4 = '], ['6 - 4 = ', '2 - 2 = ', '8 - 5 = ', '9 - 5 = '], ['8 - 5 = ', '6 - 2 = ', '8 - 1 = ', '2 - 1 = '], ['7 - 7 = ', '8 - 4 = ', '7 - 4 = ', '7 - 4 = '], ['8 - 8 = ', '5 - 5 = ', '6 - 2 = ', '5 - 1 = '], ['9 - 1 = ', '9 - 8 = ', '7 - 6 = ', '6 - 2 = '], ['8 - 8 = ', '8 - 1 = ', '4 - 2 = ', '8 - 1 = ']]

小孩子主要从整除开始练习,所以就不要搞什么4÷3这样的了。所以也加入了限制条件。首先,被除数要大于等于除数(除号前面的数要大于等于后面的数);其次,要能整除,不要有余数。所以增加了两个条件,即num1 >= num2和num1 % num2 == 0。%指求模运算,即求除法的余数,要满足整除,则余数必须是0。

#除法题import randomdata = [] #储存题目的列表group = [] #中转列表while len(data) < 20:    num1 = random.randint(1,19)    num2 = random.randint(1,19)    if num1 >= num2 and num1 % num2 == 0:        group.append(f"{num1} ÷ {num2} = ") #数据放入中转列表        if len(group) == 4: #按四道题分一组            data.append(group) #数据放入总列表            group = []#清空中转列表data[['8 ÷ 2 = ', '12 ÷ 1 = ', '8 ÷ 8 = ', '3 ÷ 1 = '], ['10 ÷ 5 = ', '11 ÷ 11 = ', '14 ÷ 2 = ', '16 ÷ 16 = '], ['7 ÷ 7 = ', '12 ÷ 4 = ', '8 ÷ 2 = ', '11 ÷ 11 = '], ['9 ÷ 3 = ', '2 ÷ 2 = ', '3 ÷ 1 = ', '10 ÷ 10 = '], ['12 ÷ 4 = ', '11 ÷ 11 = ', '12 ÷ 1 = ', '14 ÷ 2 = '], ['13 ÷ 13 = ', '12 ÷ 12 = ', '2 ÷ 1 = ', '15 ÷ 5 = '], ['7 ÷ 7 = ', '7 ÷ 7 = ', '7 ÷ 1 = ', '9 ÷ 3 = '], ['4 ÷ 1 = ', '18 ÷ 6 = ', '12 ÷ 12 = ', '6 ÷ 1 = '], ['12 ÷ 12 = ', '2 ÷ 2 = ', '1 ÷ 1 = ', '15 ÷ 5 = '], ['16 ÷ 2 = ', '18 ÷ 6 = ', '5 ÷ 5 = ', '14 ÷ 2 = '], ['17 ÷ 1 = ', '13 ÷ 1 = ', '14 ÷ 14 = ', '7 ÷ 1 = '], ['16 ÷ 1 = ', '18 ÷ 18 = ', '3 ÷ 3 = ', '2 ÷ 1 = '], ['14 ÷ 1 = ', '6 ÷ 3 = ', '2 ÷ 1 = ', '14 ÷ 1 = '], ['8 ÷ 2 = ', '16 ÷ 8 = ', '9 ÷ 3 = ', '18 ÷ 1 = '], ['19 ÷ 1 = ', '4 ÷ 4 = ', '15 ÷ 1 = ', '10 ÷ 1 = '], ['17 ÷ 17 = ', '12 ÷ 1 = ', '9 ÷ 1 = ', '12 ÷ 2 = '], ['17 ÷ 1 = ', '4 ÷ 1 = ', '2 ÷ 2 = ', '19 ÷ 19 = '], ['8 ÷ 4 = ', '6 ÷ 6 = ', '16 ÷ 2 = ', '3 ÷ 1 = '], ['3 ÷ 1 = ', '6 ÷ 1 = ', '11 ÷ 11 = ', '18 ÷ 9 = '], ['4 ÷ 4 = ', '6 ÷ 6 = ', '10 ÷ 1 = ', '13 ÷ 1 = ']]

数据准备好后,就可以将这些题目写入到word文档,以便打印给孩子练手了。如下定义了一个函数To_word(),用于将数据写入word文档,并保存。其中有两个参数,data为包含数据的列表,file_name为word文档的文件名。使用Document()新建一个word文件,并存入doc变量,然后遍历数据列表data,将子列表中包含的数据按两个tab(产生空格)的间隔连接在一起"\t\t".join(i),然后新建段落,并写入到word文件,即doc.add_paragraph(row)。写入完成后,将所有内容的字号设置为16磅。最后按照指定的文件名保存数据。

#输出到word文件def To_word(data,file_name):    '''    data:需要传入的列表    file_name:word文件的文件名    '''    from docx import Document    from docx.shared import Pt #用于设定字体大小(磅值)    doc = Document()    for i in data:        row = "\t\t".join(i)        doc.add_paragraph(row)    #设置字号    for para in doc.paragraphs:         for run in para.runs:            run.font.size = Pt(16) #文字大小磅值    doc.save(f"{file_name}.docx")

然后,运行相应的数据获取程序,再调用To_word()函数,就可得到需要的word文档了。再打印出来,就可以开始虐孩之旅了。

To_word(data,"加法题")

PythonOffice专注于办公自动化,使用Python语言操作Excel、Word 、PPT、PDF等Office软件,代替日常工作中的重复机械操作,千倍提升工作效率。更多工作中的实例即源文件下载请到微信公众号“Python操作Office软件高效工作”。

标签: #python数学代码