|

楼主 |
发表于 2022-3-20 16:59:35
|
显示全部楼层
- #easygui版
- from random import *
- from decimal import Decimal
- from easygui import *
- class equation:
- def __init__(self,fomula,answer):
- self.fomula = fomula
- self.answer = answer
- def ask(self,t):
- try:
- split = enterbox(self.fomula+'\n'+"请输入你的答案(x是多少)(有多个解用 / 隔开):",title = "第{0}题".format(t)).split("/")
- except:
- return 'exit'
- while True:
- try:
- ans = [float(i.replace(" ", "")) for i in split]
- except:
- try:
- split = enterbox("输入错误,请重新输入:",title = "第{0}题".format(t)).split("/")
- except:
- return 'exit'
- else:
- break
- if ans == self.answer:
- c = msgbox("恭喜你,答对了!",title = "第{0}题".format(t))
- if c:
- return True
- else:
- return 'exit'
- else:
- c = msgbox("答错了,正确答案是"+str(*self.answer),title = "第{0}题".format(t))
- if c:
- return True
- else:
- return 'exit'
- def get_equation1(level):
-
- if level == 1:
- # ax = b 类型的方程
- a = randint(4,20)
- x = randint(4,20)
- b = a * x
- fomula = str(a)+"x = "+str(b)
- temp = equation(fomula,[x])
- return temp
- elif level == 2:
- # ax = b 类型的方程,但是a x是小数
- a = randint(4,20) + randint(1,9)/10
- x = randint(4,20)
- b = float(Decimal(str(a)) * Decimal(str(x)))# 使用decimal模块里的Decimal函数
- fomula = str(a)+"x = "+str(b)
- temp = equation(fomula,[x])
- return temp
- elif level == 3:
- # ax + b = c 类型的方程
- a = randint(4,20)
- x = randint(4,20)
- b = randint(4,20)
- c = a * x + b
- fomula = str(a)+"x + "+str(b)+" = "+str(c)
- temp = equation(fomula,[x])
- return temp
- elif level == 4:
- # ax + b = c 类型的方程, a x b是小数
- a = randint(4,20) + randint(1,9)/10
- x = randint(4,20)
- b = randint(4,20) + randint(1,9)/10
- c = float(Decimal(str(a)) * Decimal(str(x)) + Decimal(str(b))) # 使用decimal模块里的Decimal函数
- fomula = str(a)+"x + "+str(b)+" = "+str(c)
- temp = equation(fomula,[x])
- return temp
-
- def main():
- correct = 0
- for i in range(1,5):
- temp = get_equation1(i)
- t = temp.ask(2*i-1)
- if t and t!='exit':
- correct += 1
- elif t == 'exit':
- return 0
- temp = get_equation1(i)
- t = temp.ask(2*i)
- if t and t!='exit':
- correct += 1
- elif t == 'exit':
- return 0
- correct = 0
- msgbox("你答对了{0}道题,正确率是{1}%".format(correct,int(correct*10)))
- if __name__ == '__main__':
- main()
复制代码 |
|