生成小学计算题

一、Github项目地址:

https://github.com/ReganWhiteLin/Test/blob/master/%E5%B0%8F%E5%AD%A6%E8%AE%A1%E7%AE%97%E9%A2%98

 

二、各模块耗费时间估计

 

模块

耗费时间(单位:h)

项目要求分析

0.5

编写代码

1

项目测试及优化

2

博客撰写

2


三、解题思路

 

由于题目属于基本的小学算术题,只需满足输出题目,输入答案,并输出验证成果,步骤较少且难度不算高,因此直接开始编写代码。

 

四、设计过程

1)生成一个大于1且不大于5的正整数以决定计算题中数的个数;

2)随机生成(1)中生成数个数的正整数(小于100);

3)随机生成若干个计算符号作用于(2)中生成数之间的计算;

4)输入学生自己的计算成果并验证其对错;

5)输入y循环上述过程,或输入n结束循环;

 

五、代码说明

 

 1 @author: Regan_White
 2 """
 3 
 4 from math import *
 5 from random import *
 6 from fractions import *
 7 
 8 choice='y'
 9 while choice=='y':            #循环以不断出题
10     equa=''
11     num=randint(2,5)          #生成随机数以确定本次题目计算数的个数
12     a=[randint(1,100) for i in range(num)]      
13     for i in range(num):        #生成随机数以计算
14         a[i]=randint(1,100)
15     total=a[0]
16     for j in range(num-1):
17         flag=randint(1,4)       #生成随机数以决定计算符号,如生成1则为加法,2为减法,以此类推
18         if flag==1:        
19             total=total+a[j+1]
20             equa+=str(a[j])+'+'
21         elif flag==2:      
22             total=total-a[j+1]
23             equa+=str(a[j])+'-'
24         elif flag==3:      
25             total=total*a[j+1]
26             equa+=str(a[j])+'×'
27         elif flag==4:      
28             total = Fraction(total, a[j+1]) 
29             equa+=str(a[j])+'÷'
30     equa+=str(a[j+1])+'='
31     ans=str(input(equa))   #输出题目并输入学生回答
32     if ans==str(total):           #验证结果,如正确则输出回答正确,错误则输出回答错误并输出正确结果
33         print('回答正确')
34     else:
35         print("回答错误,正确答案为"+str(total))
36     choice=input("按y以继续出题,按n结束
")          #输入y或n以决定是否继续出题或结束出题

 

六、测试运行

 

七、改进程序性能所花费时间

  

1906 function calls (1886 primitive calls) in 0.016 seconds

 

Ordered by: standard name

 

ncalls

tottime

percall

cumtime

percall

filename:lineno(function)

1

0.000

0.000

0.016

0.016

9_20.py:13(fun)

 

 

八、各模块实际耗费时间

 

模块

耗费时间(单位:h)

项目要求分析

0.2

编写代码

1

项目测试及优化

2

博客撰写

1


 
 

原文地址:https://www.cnblogs.com/ReganWhite/p/13700334.html