小学算术题四则运算
网址:https://github.com/hjx554/
题目要求:
自动生成小学四则运算算术题,并进行验证题目难度
解题思路:
1.运算符随机生成.
2.题目随机生成
3.自动检验题目的对错
程序设计:
我们要生成简单的四则运算题目需要两个整数或是分数,为了将答案与算式分类,我们分别用两个列表来储存。
我们可以通过random模块生成一定区间里的随机数,分数也可以通过随机生成分子和分母的形式随机生成。同时分数可以使用Fraction模块,可以同时实现分数与整数的运算以及分子和分母的约分。
代码实现:
def c1(q, ans): symbol = random.choice(['+', '-', '*', '/']) # 生成随机符号 if symbol == '+': n1 = random.randint(0, 20) n2 = random.randint(0, 20) q.append(str(n1) + '+' + str(n2) + '=') ans.append(n1 + n2) elif symbol == '-': n1 = random.randint(0, 20) n2 = random.randint(0, 20) n1,n2 = max(n1,n1),min(n1,n2)#防止出现负数 q.append(str(n1) + '-' + str(n2) + '=') ans.append(n1 - n2) elif symbol == '*': n1 = random.randint(0, 20) n2 = random.randint(0, 20) q.append(str(n1) + '×' + str(n2) + '=') ans.append(n1 * n2) else: n1 = random.randint(0, 20) if n1 == 0: n2 = random.randint(1, 20) else: n2 = random.randint(1, n1 + 1) q.append(str(n1) + '÷' + str(n2) + '=') ans.append(Fraction(n1, n2))
def createF(): #生成分数 fz1 = random.randint(0, 20) if fz1 == 0: fm1 = random.randint(1, 20) else: fm1 = random.randint(1, 20) f1 = Fraction(fz1, fm1) fz2 = random.randint(1, 20) fm2 = random.randint(20, 20) f2 = Fraction(fz2, fm2) return f1, f2
def c2(q,ans): #两个分数的四则运算 symbol = random.choice(['+','-','*','/']) f1,f2 = createF() if symbol =='+': while f1+f2>1: f1,f2 = createF() q.append(str(f1)+'+'+str(f2)+'=') ans.append(f1+f2) elif symbol =='-': f1,f2 = max(f1,f2),min(f1,f2) #防止出现负数 q.append(str(f1)+'-'+str(f2)+'=') ans.append(f1-f2) elif symbol == '*': while f1*f2>1: f1,f2 = createF() q.append(str(f1)+'×'+str(f2)+'=') ans.append(f1*f2) else: while f1/f2>1: f1,f2=createF() q.append(str(f1)+'÷'+str(f2)+'=') ans.append(Fraction(f1,f2))
PSP 2.1
|
PSP2.1 |
任务内容 |
计划完成需要的时间(min) |
实际完成需要的时间(min) |
|
Planning |
计划 |
50 |
60 |
|
Estimate |
估计这个任务需要多少时间,并规划大致工作步骤 |
20 |
30 |
|
Analysis |
需求分析 (包括学习新技术) |
60 |
90 |
|
Design |
具体设计 |
50 |
60 |
|
Coding |
具体编码 |
400 |
500 |
|
test |
测试(自我测试,修改代码,提交修改) |
200 |
250 |
|
Postmortem & Process Improvement Plan |
事后总结 ,并提出过程改进计划 |
30 |
50 |
|
Summary |
合计 |
810 |
1040 |
代码实现
import random #四则运算 def szys(): sym = ['+', '-', '×', '÷'] f= random.randint(0, 3) n1 = random.randint(1, 20) n2 = random.randint(1, 20) result = 0 if f== 0:#加法 result = n1 + n2 elif f == 1:#减法,要先比较大小,防止输出负数 n1, n2 = max(n1, n2), min(n1, n2) result = n1 - n2 elif f== 2:#乘法 result = n1 * n2 elif f == 3:#除法,要比较大小,并循环取整除 n1, n2 = max(n1, n2), min(n1, n2) while n1 % n2 != 0: n1 = random.randint(1, 10) n2 = random.randint(1, 10) n1, n2 = max(n1, n2), min(n1, n2) result = int(n1 / n2) print(n1, sym[f], n2, '= ', end='') return result #制作题库 def test(): sym = ['+', '-', '×', '÷'] print('输入所需要的题目数量') n=int(input()) result =[] m=0 while m<=(n-1): print(m+1,end='、') result .append(szys()) print(' ') m=m+1 m=0 print('对应的答案:') while m<=(n-1): print(m+1,'、',result [m]) m=m+1 print('选择想要的模式') print('1、进行四则运算') print('2、制作题库') n=int(input()) #当输入1时,进行四则运算,调用函数syzs() if n==1: while True: result = szys() j= input() s= int(j) if s== result : print('right') else: print('error.,the answer is', result ) #当输入2时,进行制作题库 if n==2: test()

(6)PSP表格
|
|
|
预计耗时(分钟) |
是实际耗时(分钟) |
|
Planning |
计划 |
20 |
15 |
|
Estimate |
估计这个任务需要多少时间 |
/ |
/ |
|
Development |
开发 |
120 |
200 |
|
Analysis |
需求分析 |
5 |
7 |
|
Design Spec |
生成设计文档 |
/ |
/ |
|
Design Review |
设计复审(和同事审核设计文档) |
/ |
/ |
|
Coding Standerd |
代码规范(为目前的开发制定合适的规范) |
5 |
8 |
|
Design |
具体设计 |
8 |
12 |
|
Coding |
具体编码 |
35 |
70 |
|
Code Review |
代码复审 |
20 |
30 |
|
Text |
测试(自测,修改代码,提交修改) |
20 |
30 |
|
Reporting |
报告 |
25 |
20 |
|
Text Report |
测试报告 |
10 |
15 |
|
Size Measurement |
计算工作量 |
6 |
5 |
|
Postmortem & Process Improvement Plan |
事后总结,并提出过程改进计划 |
5 |
5 |
|
Sum |
合计 |
279 |
407 |
浙公网安备 33010602011771号