用python生成小学生一年级算术模板

2023-10-08 02:50

本文主要是介绍用python生成小学生一年级算术模板,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

孩子学习口算压力山大,现在的要求太高了需要5分钟内答满150道题,完成这个目标需要多多的练习。
加法要求和不能大于21,考虑到20以上的进位问题,孩子目前还是不熟练没有添加进去。减法差不能是负数,被减数是个位数的。除法先取乘法的商作为被除数,这样就可以被整除了。还加入了左右括号的填空,灵活转换被加数和被减数的算法。由于输出的加减乘除符号字符会因为字体不同而有差异,在pycharm的控制台输出是整齐排列的,而复制到word就会各种不对齐,对比使用Courier New字体排列的最完美了。所以复制到word要修改一下字体,达到强迫症的对齐要求。

# coding=utf-8
import randomresult = []
operList = ["+", "-"]  # 如果需要乘法和除法则替换为["+", "-", "*", "/"]
unique = []  # 用于统计去重print("\t\t 姓名____________   用时___________   正确率_____________  \n ")
def generate_num():a = random.randint(1, 20)b = random.randint(0, 20)  # 100以内加法 可以 b = random.randint(0, 100-a)oper = random.choice(operList)if oper == "-":b = random.randint(0, 10)if a < b:a, b = b, aans = eval('%d%s%d' % (a, oper, b))oper = "-"elif oper == "+":if a > 10 and b > 10:a -= 10b -= 10elif a + b > 20:if a > b:a -= 10else:b -= 10ans = eval(str(a) + oper + str(b))elif oper == "*":a = random.randint(1, 9)b = random.randint(0, 9)ans = a * boper = "×"elif oper == "/":a = random.randint(0, 9)b = random.randint(1, 9)  # 被除数不能为0ans = a * boper = "÷"if str(a) + oper + str(b) in unique:  #用于去重,不能保证完全没有重复a, b, oper, ans = generate_num()unique.append(str(a) + str(oper) + str(b))return a, b, oper, ansfor i in range(25):  # 常规题目要几行for g in range(5):  # 每行要几个算式a, b, oper, ans = generate_num()if oper == "÷":print(str(ans).ljust(2, ' ') + str(oper) + str(b).rjust(2, ' ') + " =   ", end="     ")  # 对齐打印,不换行result.append(str(a))else:print(str(a).ljust(2, ' ') + str(oper) + str(b).rjust(2, ' ') + " =   ", end="     ")result.append(str(ans))# 也可以将完整题目和答案加入 result.append(str(a) + str(oper) + str(b) + "=" + str(eval('%d%s%d' % (a, oper, b))))print()  # 输出五个算式换行for i in range(6):  # 带括号题目要几行for g in range(5):  # 每行要几个算式a, b, oper, ans = generate_num()if oper == "÷":right_blank = str(ans).ljust(2, ' ') + str(oper) + '(   )' + "=" + str(a).ljust(3, ' ')left_blank = '(   )' + str(oper) + str(b).rjust(2, ' ') + "=" + str(a).ljust(3, ' ')else:right_blank = str(a).ljust(2, ' ') + str(oper) + '(   )' + "=" + str(ans).ljust(3, ' ')left_blank = '(   )' + str(oper) + str(b).rjust(2, ' ') + "=" + str(ans).ljust(3, ' ')blank = random.choice([right_blank, left_blank])  # 括号在左边还是右边# 根据左边右边的括号不同整理答案if oper == "÷":if blank == left_blank:result.append(str(ans))else:if blank == left_blank:result.append(str(a))if blank == right_blank:result.append(str(b))print(blank, end="  ")print()# 打印答案
print("\n" + "*" * 28 + "以下为答案" + "*" * 28)
for i, v in enumerate(result):if i % 5 == 0:print()print(v.ljust(2, " "), end="             ")

实验一下输出结果为包含加减乘除:

7 +10 =     5 - 3 =     27÷ 9 =     8 + 6 =     0 ÷ 4 =     
0 ÷ 9 =     2 + 3 =     3 + 4 =     2 × 4 =     40÷ 8 =     
5 × 4 =     6 × 9 =     1 +16 =     8 + 3 =     10+ 7 =     
8 + 4 =     9 × 8 =     7 ÷ 7 =     4 + 2 =     40÷ 5 =     
13+ 5 =     4 × 1 =     3 × 1 =     18- 0 =     10+ 2 =     
3 +14 =     1 + 7 =     56÷ 8 =     9 × 1 =     0 ÷ 9 =     
13- 1 =     16-10 =     8 × 6 =     0 ÷ 1 =     10+ 5 =     
10+ 4 =     1 × 2 =     3 + 9 =     3 × 9 =     20÷ 4 =     
7 - 7 =     6 - 1 =     3 × 9 =     18÷ 3 =     30÷ 5 =     
36÷ 6 =     7 × 2 =     21÷ 3 =     7 ÷ 1 =     15-10 =     
3 × 7 =     9 ÷ 3 =     12- 4 =     7 × 8 =     56÷ 7 =     
9 + 1 =     5 ÷ 1 =     5 - 0 =     0 × 3 =     9 +10 =     
9 - 5 =     3 +17 =     3 × 0 =     4 × 4 =     14÷ 2 =     
18-10 =     6 ÷ 6 =     8 - 7 =     18÷ 3 =     45÷ 5 =     
0 ÷ 3 =     6 ÷ 1 =     18÷ 3 =     9 - 0 =     1 × 8 =     
45÷ 9 =     8 × 5 =     4 × 5 =     7 × 5 =     8 + 9 =     
7 × 8 =     1 × 4 =     9 - 6 =     10+ 5 =     7 - 4 =     
5 +11 =     1 +13 =     10- 3 =     3 ÷ 3 =     8 - 2 =     
14- 6 =     7 - 1 =     16- 2 =     5 + 9 =     7 + 4 =     
5 × 0 =     2 - 0 =     9 - 0 =     6 ÷ 2 =     11- 8 =     
16- 5 =     3 +10 =     9 × 5 =     42÷ 6 =     3 × 9 =     
3 +17 =     20- 9 =     3 × 3 =     19- 9 =     10+ 4 =     
9 + 3 =     8 × 4 =     20÷ 4 =     2 + 5 =     8 +11 =     
10- 7 =     1 × 0 =     2 × 2 =     18÷ 9 =     5 ÷ 5 =     
42÷ 6 =     8 × 9 =     20- 8 =     2 + 5 =     4 × 4 =     
6 ÷ 1 =     40÷ 5 =     8 ÷ 1 =     5 +10 =     6 × 9 =     
10+ 1 =     6 × 2 =     5 - 1 =     9 × 4 =     10- 7 =     
1 +17 =     5 × 4 =     12÷ 3 =     0 × 4 =     20÷ 5 =     
42÷ 6 =     6 × 9 =     3 + 4 =     7 × 1 =     0 ÷ 2 =     
3 +15 =     4 × 0 =     5 × 7 =     40÷ 5 =     5 - 2 =     
( )+ 4=15   ( )- 1=1    10+( )=14   5 -( )=3    ( )÷ 2=4    
( )÷ 5=7    ( )+ 9=15   2 ×( )=16   ( )÷ 6=6    ( )- 5=1    
( )- 5=7    42÷( )=6    0 -( )=0    4 +( )=5    ( )× 5=30   
( )× 1=3    17+( )=18   ( )× 1=3    ( )÷ 6=6    ( )- 6=12   
27÷( )=9    ( )- 0=3    0 +( )=18   ( )÷ 6=3    8 +( )=11   
18-( )=9    5 ×( )=15   17-( )=10   5 ×( )=35   ( )+ 7=17   17           2            3            14           0            
0            5            7            8            5            
20           54           17           11           17           
12           72           1            6            8            
18           4            3            18           12           
17           8            7            9            0            
12           6            48           0            15           
14           2            12           27           5            
0            5            27           6            6            
6            14           7            7            5            
21           3            8            56           8            
10           5            5            0            19           
4            20           0            16           7            
8            1            1            6            9            
0            6            6            9            8            
5            40           20           35           17           
56           4            3            15           3            
16           14           7            1            6            
8            6            14           14           11           
0            2            9            3            3            
11           13           45           7            27           
20           11           9            10           14           
12           32           5            7            19           
3            0            4            2            1            
7            72           12           7            16           
6            8            8            15           54           
11           12           4            36           3            
18           20           4            0            4            
7            54           7            7            0            
18           0            35           8            3            
11           2            4            2            8            
35           6            8            36           6            
12           7            0            1            6            
3            1            3            36           18           
3            3            18           18           3            
9            3            7            7            10 

![在这里插入图片描述](https://img-blog.csdnimg.cn/f0e1c14be9034f58a91bc9c1797f9667.jpeg#pic_center

口算大概是需要一步步的练习,先从没有进位的加减,接着是只涉及十位的加减,然后是个位数的有进退位的加减,最后是混合的加减。一个个阶梯拾级而上,练习进步。出现的被加减数为0的可能性比较大,去除为0的可能。随机数random.randint(a, b),a不可以大于b,否则报错ValueError: empty range for randrange() (1,1, 0),因此要加入分支条件,当a=1时,剔除b=0的情形。

int(a/10) 取出被加数的十位数, a%10 求余取出被加数的个位数

def generate_num():a = random.randint(0, 99)oper = random.choice(operList)if oper == "-":# t1是个位的减法,不涉及退位,被减数是两位数同时个位是0,则进行十位的减法if a == 0:t1 = 0elif a % 10 == 0:t1 = random.randint(1, int(a / 10)) * 10else:t1 = random.randint(1, a % 10)# t2是十位的减法,个位为零if a < 10:t2 = random.randint(1, 10)else:t2 = random.randint(1, int(a / 10)) * 10# t3是个位的减法,都是有退位的,被减数个位相同,再随机一次t3 = random.randint(a % 10,9)if t3 == a % 10:t3 = random.randint(a % 10 +1, 10)b = random.choice([ t1,t2,t3])  # b = random.randint(1, 10)if a < b:a, b = b, aans = eval('%d%s%d' % (a, oper, b))oper = "-"elif oper == "+":# p1是个位的加法,不涉及进位p1 =random.randint(1, 10 - (a % 10))# p2是十位的加法if a>=90:p2 = random.randint(1, 10 - (a % 10))else:p2 =random.randint(1, 9 - int(a / 10)) * 10# p3是个位的加法,都是有进位if a >= 90:p3 = random.randint(1, 100 -a)elif a %10 ==0: #把求余为0的可能去除掉,方便下面randint下标不越界p3 =random.randint(1, 9 - int(a / 10)) * 10else:p3 = random.randint( 10-a % 10, 9)b = random.choice( [p1,p2,p3])  ans = eval(str(a) + oper + str(b))if str(a) + oper + str(b) in unique:a, b, oper, ans = generate_num()unique.append(str(a) + str(oper) + str(b))return a, b, oper, ans

输出结果如下:可以将 b = random.choice([ t1,t2,t3]) 只剩下一个,从t1到t3,逐个打印。

 姓名____________   用时___________   正确率_____________  92-80 =        25-10 =        4 - 1 =        88+ 3 =        65+ 1 =        
92+ 8 =        75+ 8 =        20-10 =        81+10 =        46+ 5 =        
85-30 =        24+ 8 =        78-70 =        12+ 8 =        65+ 2 =        
70+20 =        85- 8 =        53+10 =        99- 2 =        72+ 9 =        
38- 9 =        95+ 1 =        51+20 =        52+ 3 =        43+ 2 =        
29- 3 =        62-20 =        44- 2 =        70+ 9 =        36+ 5 =        
35-10 =        24-20 =        80- 2 =        72+ 1 =        73+10 =        
95-80 =        89- 5 =        6 +40 =        53- 3 =        22+ 9 =        
62+ 6 =        25- 4 =        1 + 9 =        23- 3 =        17+60 =        
83+ 9 =        14+ 4 =        78+ 5 =        48- 6 =        61+ 1 =        
72+10 =        72-10 =        57+ 3 =        10- 9 =        80- 4 =        
99-10 =        41- 4 =        42- 1 =        81+ 9 =        25+ 3 =        
53-20 =        91+ 9 =        78-10 =        67- 8 =        86- 9 =        
54-40 =        88+ 5 =        13- 7 =        39+ 2 =        54+ 8 =        
59-40 =        62+10 =        32- 2 =        80- 7 =        2 - 1 =        
30+20 =        18+ 8 =        88+ 1 =        76+ 4 =        87+ 5 =        
60-10 =        31+30 =        83- 4 =        89+ 1 =        95- 2 =        
97-70 =        95- 7 =        50-10 =        4 - 4 =        2 +60 =        
34- 3 =        47+ 7 =        1 - 1 =        92+ 4 =        43- 9 =        
56- 7 =        15+ 6 =        32+ 8 =        91+ 3 =        12-10 =        
20-20 =        28- 9 =        41- 1 =        74-40 =        71-20 =        
66+30 =        32-20 =        9 - 2 =        30+ 3 =        65+20 =        
28- 8 =        55-20 =        23+ 1 =        93-30 =        65-50 =        
69+ 3 =        93- 3 =        80-50 =        29+ 8 =        46- 5 =        
45+ 6 =        23-10 =        1 + 3 =        90+ 4 =        75+20 =        
(   )+ 2=59   36+(   )=40   (   )+ 1=74   11+(   )=13   (   )+ 5=20   
(   )+ 9=96   21+(   )=61   63+(   )=70   (   )- 7=24   (   )- 2=65   
(   )+70=75   (   )+10=80   (   )+ 9=70   19+(   )=29   (   )+ 1=95   
81-(   )=80   (   )+30=85   (   )+70=85   90-(   )=80   (   )- 2=18   
85-(   )=65   (   )+30=67   41-(   )=31   (   )-70=15   (   )+ 3=40   
44+(   )=49   95-(   )=55   (   )+ 5=31   (   )+ 2=100  77-(   )=69   

如下一些参考的文章:
https://www.cnblogs.com/Lincoln-Wong/p/15348545.html
https://blog.csdn.net/AbyssssssssssS/article/details/101272303
https://blog.csdn.net/m0_63445876/article/details/122450278
https://blog.csdn.net/bosivip/article/details/127197075

这篇关于用python生成小学生一年级算术模板的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/162194

相关文章

Python将博客内容html导出为Markdown格式

《Python将博客内容html导出为Markdown格式》Python将博客内容html导出为Markdown格式,通过博客url地址抓取文章,分析并提取出文章标题和内容,将内容构建成html,再转... 目录一、为什么要搞?二、准备如何搞?三、说搞咱就搞!抓取文章提取内容构建html转存markdown

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Python Websockets库的使用指南

《PythonWebsockets库的使用指南》pythonwebsockets库是一个用于创建WebSocket服务器和客户端的Python库,它提供了一种简单的方式来实现实时通信,支持异步和同步... 目录一、WebSocket 简介二、python 的 websockets 库安装三、完整代码示例1.

揭秘Python Socket网络编程的7种硬核用法

《揭秘PythonSocket网络编程的7种硬核用法》Socket不仅能做聊天室,还能干一大堆硬核操作,这篇文章就带大家看看Python网络编程的7种超实用玩法,感兴趣的小伙伴可以跟随小编一起... 目录1.端口扫描器:探测开放端口2.简易 HTTP 服务器:10 秒搭个网页3.局域网游戏:多人联机对战4.

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优