自动生成小学四则运算
Gitee的源代码:https://github.com/asd2396012061/1/tree/master
问题:
- 能自动生成小学四则运算题目,且不能出现负数
- 除了整数外,还要支持真分数的四则运算
工具列表:
- 编程IDE:Visual Studio Code
- 编程语言:Python
- 效能分析工具:Python自带分析库cProfile
- 源代码托管平台:Github
psp表格:
PSP表格指标 | 预估耗时(分钟) | 实际耗时(分钟) |
计划-估计该任务需要多少时间 | 180 | 162 |
需求分析与思路解析 | 30 | 40 |
代码实现 | 45 | 62 |
代码测试 | 22 |
20 |
代码优化 |
20 | 43 |
写博客 | 20 | 28 |
问题的解决思路:
1.生成随机数
2.生成随机算数运算符
3.输入题目数量
4.用户输入答案
5.输出正确答案
代码:
def result(s,n1,n2): if s == '+': print('{} + {} = {}'.format(n1,n2,n1 + n2)) elif s == '-': print('{} - {} = {}'.format(n1,n2,n1 - n2)) elif s == '*': print('{} * {} = {}'.format(n1,n2,n1 * n2)) elif s == '/': if n2 == 0: print('***除数不能为零***') else: print('{} / {} = {}'.format(n1,n2,n1 / n2)) else: print('输入有误,请重新输入!')
def zsResult(): qes = int(input()) for a in range(qes): rsign = random.choice(['+','-','*','/']) rnum1 = random.randint(0,100) rnum2 = random.randint(0,100) if rnum1 < rnum2: rnum1 = max(rnum1, rnum2) rnum2 = min(rnum1, rnum2) result(rsign,rnum1,rnum2) else: result(rsign,rnum1,rnum2)
def zfsResult(): qes = int(input()) for b in range(qes): rsign = random.choice(['+','-','*','/']) snum1 = random.randint(0,100) mnum1 = random.randint(1,100) zfs1 = Fraction(snum1, mnum1) snum2 = random.randint(1,100) mnum2 = random.randint(1,100) zfs2 = Fraction(snum2, mnum2) if zfs1 < zfs2: zfs1 = max(zfs1, zfs2) zfs2 = min(zfs1, zfs2) result(rsign,zfs1,zfs2) else:
result(rsign,zfs1,zfs2
测试运算:
效能分析: