# -*- coding: utf-8 -*- """ Created on Sun Sep 20 15:04:51 2020 @author: Thinkpad """ # -*- coding: utf-8 -*- #导入random模块 import random @profile #定义产生真分数的函数 def fraction(): while(True): a=random.randint(1,100) b=random.randint(1,100) if a/b<1: return str(a)+"/"+str(b) break #定义产生带括号运算函数 def brackets(): a="(" e=")" b=str(random.randint(1,100)) c=random.choice('+-*/') d=fraction() if eval(b+c+d)>0: return a+b+c+d+e #生成并打印四则运算表达式 for i in range(30): a= brackets() b=str(random.choice('+-*/')) c=fraction() if eval(a+b+c)>=0: print(a,b,c,"="," 答案:",eval(a+b+c))
1.之前代码--性能测试(请看上篇)
2.功能增加:
增加了统计你做对了,总得分
代码展示
""" Created on Sun Sep 20 15:04:51 2020 @author: Thinkpad """ # -*- coding: utf-8 -*- #导入random模块 import random #@profile #定义产生真分数的函数 def fraction(): while(True): a=random.randint(1,100) b=random.randint(1,100) if a/b<1: return str(a)+"/"+str(b) break #定义产生带括号运算函数 def brackets(): a="(" e=")" b=str(random.randint(1,100)) c=random.choice('+-*/') d=fraction() if eval(b+c+d)>0: return a+b+c+d+e #生成并打印四则运算表达式 def main(): print("请输入想要做的题量") amount = int(input()) count = 0 for i in range(amount): a= brackets() b=str(random.choice('+-*/')) c=fraction() if eval(a+b+c)>=0: print(a,b,c,"="," :",) answer = float(input()) if answer == eval(a+b+c): print("答案正确") count = count + 1 else: print("答案错误,正确答案为:",eval(a+b+c)) print("测试得分为",float(count*(100/amount))) if __name__ == '__main__': main()
3.运行截图