首页 > 编程语言 >python 生成小学计算练习 docx

python 生成小学计算练习 docx

时间:2023-06-21 09:35:02浏览次数:63  
标签:docx random randint python 练习 second operator document first

python3 生成二年级下计算练习,有口算,有竖式。

 

import random
import osfrom docx.shared import Pt
from docx import Document

def create_page(document):
    #口算
    document.add_paragraph('一、口算')
    operators='+-×÷'
    columnsNumber = 3
    rowsNumbers = 8
    table = document.add_table(rows=rowsNumbers, cols=columnsNumber)
    for row in range(rowsNumbers):
        for col in range(columnsNumber):
            operator = random.choice(operators)
            first = random.randint(11, 100)
            second = random.randint(11, 100)
            if operator == '×' or operator == '÷':
                first = random.randint(2, 9)
                second = random.randint(2, 9)
            
            if operator == '÷':
                first=first*second
                                
            if operator == '-' or  operator == '+':
                if first < second:
                    first, second = second, first
                if second < 30:
                    first*=10
                    second*=10
                if second < 20:
                    first*=10
                    second*=10
            
            if operator == '+':
                first=first-second
            
            r = str(first) + ' ' + operator +' '+ str(second) + '='
            cell = table.cell(row, col)
            cell.text = r
            
    #竖式
    document.add_paragraph('')
    document.add_paragraph('二、竖式')
    operators='+-÷'
    columnsNumber = 5
    rowsNumbers = 7
    table = document.add_table(rows=rowsNumbers, cols=columnsNumber)
    for row in range(int(rowsNumbers/4)+1):
        for col in range(int(columnsNumber/2)+1):
            operator = random.choice(operators)
            first = random.randint(100, 1000)
            second = random.randint(100, 1000)
            if operator == '÷':
                first = random.randint(2, 9)
                second = random.randint(2, 9)
                residue = random.randint(1, second-1)
                first=first*second+residue
                                
            if operator == '-' or  operator == '+':
                if first < second:
                    first, second = second, first
            
            if operator == '+':
                first=first-second
                        
            r = str(first) + ' ' + operator +' '+ str(second) + '='
            cell = table.cell(row*4, col*2)
            cell.text = r
    

def create_docx():
    document = Document()
    document.styles['Normal'].font.name = '微软雅黑'
    document.styles['Normal'].font.size = Pt(12)
    
    pageSize=10 # 页数
    for i in range(0,pageSize):
        if i!=0:
            document.add_page_break()
        create_page(document)
        
    document.save('二年级下计算练习.docx')
    os.startfile("二年级下计算练习.docx")

if __name__ == '__main__':
    create_docx()

 

标签:docx,random,randint,python,练习,second,operator,document,first
From: https://www.cnblogs.com/SamFang/p/17495417.html

相关文章

  • python编写下载小工具。下载YouTube视频
    确保下载环境pipinstallpytubefrompytubeimportYouTube#1.导入工具包。如果没有就下载\fromtqdmimporttqdm#2.定义下载函数defdownload_YouTube_video(url):try:yt=YouTube(url)#创建下载链接。这段代码使用提供url创建一个YouTube对象......
  • python: Reading and Writing JSON to a File
     #importjsonjsondata={'sqlserver':[{"server":"DESKTOP-NQK85G5\GEOVIN2008","useid":"sa","password":"......
  • [复习随笔]python_dcgan网络复习小知识:模型定义
    定义参数dataroot-thepathtotherootofthedatasetfolder.Wewilltalkmoreaboutthedatasetinthenextsection.workers-thenumberofworkerthreadsforloadingthedatawiththeDataLoader.batch_size-thebatchsizeusedintraining.TheD......
  • python: Loop Tuples
     defselectSql(cls):""":return:"""studentlist=[StudentListInfo.StudentList]students=[]data=cls.studentlist.selectSql()(studentlist)=data#如C#强制转......
  • Python SQLite开发
    安装$sudoapt-getupdate$sudoapt-getinstallsqlite3libsqlite3-dev$sqlite3--version3.40.12022-12-2814:03:47df5c253c0b3dd24916e4ec7cf77d3db5294cc9fd45ae7b9c5e82ad8197f38a24$sqlite3>createtabletb1(idint,namechar(10));>insert......
  • Python魔术方法详解
    前言魔术方法(MagicMethod)是Python内置方法,格式为"方法名",不需要主动调用,存在目的是为了给Python的解释器进行调用,几乎每个魔术方法都有一个对应的内置函数,或者运算符,当我们对这个对象使用这些函数或者运算符时就会调用类中的对应的魔术方法,可以理解为重写这些python的内置函数。......
  • 用Python写了一个「拥抱梅西」的小游戏
    大家好,欢迎来到Crossin的编程教室! 最近有个小伙儿因为在比赛中冲进场地拥抱梅西而出名了。 这种行为当然不可取,他也为此付出了代价。但要说我没有一丁点羡慕和佩服那是不可能的。于是,我也尬蹭一下这个热点,用Python写了一个「拥抱梅西」的小游戏。游戏效果是这样的:  游......
  • 使用python对AWS-CloudTrail-Json-日志文件key字段名称的提取
    关于AWS CloudTrail-在Console界面,默认只能看到最近90天的数据如果需要追踪更早的操作记录,得需要配置CloudTrail日志输出保存到s3在s3上不同的region位于不同的目录,最后会将某一天的日志,存放到那一天的目录/文件夹中,目录路径格式(部分)如:aws-account-xxx/CloudTrail/cn-nort......
  • Python asyncio 库源码分析
    Pythonasyncio库源码分析前言本着「路漫漫其修远兮,吾将上下而求索」的精神。终于要开始深入研究Python中asyncio的源码实现啦。本文章可能篇幅较长,因为是逐行分析asyncio的实现,也需要读者具有一定的asyncio编码经验和功底,推荐刚开始踏上Python异步编程之旅的朋......
  • Python 列表生成式(转载)
    Python列表生成式列表生成式列表生成式即ListComprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式如何生成[1,2,3,4,5,6,7,8,9,10]列表?>>>list(range(1,11))[1,2,3,4,5,6,7,8,9,10]如何生成[1x1,2x2,3x3,...,10x10]......