本文主要是介绍用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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
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
口算大概是需要一步步的练习,先从没有进位的加减,接着是只涉及十位的加减,然后是个位数的有进退位的加减,最后是混合的加减。一个个阶梯拾级而上,练习进步。出现的被加减数为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生成小学生一年级算术模板的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!