鱼C论坛

 找回密码
 立即注册
查看: 3342|回复: 28

[作品展示] 一个可以帮助你练习解方程的程序

[复制链接]
发表于 2022-3-20 14:53:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 tommyyu 于 2022-3-20 19:27 编辑
  1. from random import *
  2. from decimal import Decimal
  3. correct = 0

  4. class equation:
  5.     def __init__(self,fomula,answer):
  6.         self.fomula = fomula
  7.         self.answer  = answer
  8.     def ask(self):
  9.         print(self.fomula)
  10.         ans = [float(i.replace(' ','')) for i in input("请输入你的答案(x是多少)(有多个解用 / 隔开):").split('/')]
  11.         if ans == self.answer:
  12.             print("恭喜你,答对了!")
  13.             return True
  14.         else:
  15.             print("答错了,正确答案是"+str(self.answer))
  16.             return False

  17. def get_equation1(level):
  18.    
  19.     if level == 1:
  20.         # ax = b 类型的方程
  21.         a = randint(4,20)
  22.         x = randint(4,20)
  23.         b = a * x
  24.         fomula = str(a)+"x = "+str(b)

  25.         temp = equation(fomula,[x])
  26.         return temp

  27.     elif level == 2:
  28.         # ax = b 类型的方程,但是a x是小数
  29.         a = randint(4,20) + randint(1,9)/10
  30.         x = randint(4,20)
  31.         b = float(Decimal(str(a)) *  Decimal(str(x)))# 使用decimal模块里的Decimal函数
  32.         fomula = str(a)+"x = "+str(b)

  33.         temp = equation(fomula,[x])
  34.         return temp

  35.     elif level == 3:
  36.         # ax + b = c 类型的方程
  37.         a = randint(4,20)
  38.         x = randint(4,20)
  39.         b = randint(4,20)
  40.         c = a * x + b
  41.         fomula = str(a)+"x + "+str(b)+" = "+str(c)

  42.         temp = equation(fomula,[x])
  43.         return temp

  44.     elif level == 4:
  45.         # ax + b = c 类型的方程, a x b是小数
  46.         a = randint(4,20) + randint(1,9)/10
  47.         x = randint(4,20)
  48.         b = randint(4,20) + randint(1,9)/10
  49.         c = float(Decimal(str(a)) *  Decimal(str(x)) + Decimal(str(b))) # 使用decimal模块里的Decimal函数
  50.         fomula = str(a)+"x + "+str(b)+" = "+str(c)

  51.         temp = equation(fomula,[x])
  52.         return temp
  53.    
  54. def main():
  55.     for i in range(1,5):
  56.         temp = get_equation1(i)
  57.         if temp.ask():
  58.             correct += 1
  59.         temp = get_equation1(i)
  60.         if temp.ask():
  61.             correct += 1
  62.     print("你答对了{0}道题,正确率是{1}%".format(correct,int(correct*10)))

  63. if __name__ == '__main__':
  64.     main()
复制代码

注:1.该程序只能练习一元一次方程 (也许下次可以改进
      2.该程序不能挑选难度(也许下次可以改进
      3.欢迎大家来挑错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-20 15:31:19 | 显示全部楼层

回帖奖励 +1 鱼币

改进了一下
  1. from random import *
  2. from decimal import Decimal

  3. correct = 0


  4. class equation:
  5.     def __init__(self, formula, answer):
  6.         self.formula = formula
  7.         self.answer = answer

  8.     def ask(self):
  9.         print(self.formula)
  10.         split = input("请输入你的答案(x是多少)(有多个解用 / 隔开):").split("/")
  11.         while True:
  12.             try:
  13.                 ans = [float(i.replace(" ", "")) for i in split]
  14.             except:
  15.                 split = input("输入错误,请重新输入:").split("/")
  16.             else:
  17.                 break
  18.         if ans == self.answer:
  19.             print("恭喜你,答对了!")
  20.             return True
  21.         else:
  22.             print("答错了,正确答案是: ", end="")
  23.             print(*self.answer, sep=", ")
  24.             return False


  25. def get_equation1(level):

  26.     if level == 1:
  27.         # ax = b 类型的方程
  28.         a = randint(4, 20)
  29.         x = randint(4, 20)
  30.         b = a * x
  31.         formula = str(a) + "x = " + str(b)

  32.         temp = equation(formula, [x])
  33.         return temp

  34.     elif level == 2:
  35.         # ax = b 类型的方程,但是a x是小数
  36.         a = randint(4, 20) + randint(1, 9) / 10
  37.         x = randint(4, 20)
  38.         b = float(Decimal(str(a)) * Decimal(str(x)))  # 使用decimal模块里的Decimal函数
  39.         formula = str(a) + "x = " + str(b)

  40.         temp = equation(formula, [x])
  41.         return temp

  42.     elif level == 3:
  43.         # ax + b = c 类型的方程
  44.         a = randint(4, 20)
  45.         x = randint(4, 20)
  46.         b = randint(4, 20)
  47.         c = a * x + b
  48.         formula = str(a) + "x + " + str(b) + " = " + str(c)

  49.         temp = equation(formula, [x])
  50.         return temp

  51.     elif level == 4:
  52.         # ax + b = c 类型的方程, a x b是小数
  53.         a = randint(4, 20) + randint(1, 9) / 10
  54.         x = randint(4, 20)
  55.         b = randint(4, 20) + randint(1, 9) / 10
  56.         c = float(
  57.             Decimal(str(a)) * Decimal(str(x)) + Decimal(str(b))
  58.         )  # 使用decimal模块里的Decimal函数
  59.         formula = str(a) + "x + " + str(b) + " = " + str(c)

  60.         temp = equation(formula, [x])
  61.         return temp


  62. def main():
  63.     global correct

  64.     for i in range(1, 5):
  65.         temp = get_equation1(i)
  66.         if temp.ask():
  67.             correct += 1
  68.         temp = get_equation1(i)
  69.         if temp.ask():
  70.             correct += 1
  71.     print("你答对了{0}道题,正确率是{1}%".format(correct, int(correct * 10)))


  72. if __name__ == "__main__":
  73.     main()
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 贡献 +3 收起 理由
tommyyu + 3 + 3 + 3 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-20 16:59:35 | 显示全部楼层
  1. #easygui版
  2. from random import *
  3. from decimal import Decimal
  4. from easygui import *

  5. class equation:
  6.     def __init__(self,fomula,answer):
  7.         self.fomula = fomula
  8.         self.answer  = answer
  9.     def ask(self,t):
  10.         try:
  11.             split = enterbox(self.fomula+'\n'+"请输入你的答案(x是多少)(有多个解用 / 隔开):",title = "第{0}题".format(t)).split("/")
  12.         except:
  13.             return 'exit'
  14.         while True:
  15.             try:
  16.                 ans = [float(i.replace(" ", "")) for i in split]
  17.             except:
  18.                 try:
  19.                     split = enterbox("输入错误,请重新输入:",title = "第{0}题".format(t)).split("/")
  20.                 except:
  21.                     return 'exit'
  22.             else:
  23.                 break
  24.         if ans == self.answer:
  25.             c = msgbox("恭喜你,答对了!",title = "第{0}题".format(t))
  26.             if c:
  27.                 return True
  28.             else:
  29.                 return 'exit'
  30.         else:
  31.             c = msgbox("答错了,正确答案是"+str(*self.answer),title = "第{0}题".format(t))
  32.             if c:
  33.                 return True
  34.             else:
  35.                 return 'exit'

  36. def get_equation1(level):
  37.    
  38.     if level == 1:
  39.         # ax = b 类型的方程
  40.         a = randint(4,20)
  41.         x = randint(4,20)
  42.         b = a * x
  43.         fomula = str(a)+"x = "+str(b)

  44.         temp = equation(fomula,[x])
  45.         return temp

  46.     elif level == 2:
  47.         # ax = b 类型的方程,但是a x是小数
  48.         a = randint(4,20) + randint(1,9)/10
  49.         x = randint(4,20)
  50.         b = float(Decimal(str(a)) *  Decimal(str(x)))# 使用decimal模块里的Decimal函数
  51.         fomula = str(a)+"x = "+str(b)

  52.         temp = equation(fomula,[x])
  53.         return temp

  54.     elif level == 3:
  55.         # ax + b = c 类型的方程
  56.         a = randint(4,20)
  57.         x = randint(4,20)
  58.         b = randint(4,20)
  59.         c = a * x + b
  60.         fomula = str(a)+"x + "+str(b)+" = "+str(c)

  61.         temp = equation(fomula,[x])
  62.         return temp

  63.     elif level == 4:
  64.         # ax + b = c 类型的方程, a x b是小数
  65.         a = randint(4,20) + randint(1,9)/10
  66.         x = randint(4,20)
  67.         b = randint(4,20) + randint(1,9)/10
  68.         c = float(Decimal(str(a)) *  Decimal(str(x)) + Decimal(str(b))) # 使用decimal模块里的Decimal函数
  69.         fomula = str(a)+"x + "+str(b)+" = "+str(c)

  70.         temp = equation(fomula,[x])
  71.         return temp
  72.    
  73. def main():
  74.     correct = 0
  75.     for i in range(1,5):
  76.         temp = get_equation1(i)
  77.         t = temp.ask(2*i-1)
  78.         if t and t!='exit':
  79.             correct += 1
  80.         elif t == 'exit':
  81.             return 0
  82.         temp = get_equation1(i)
  83.         t = temp.ask(2*i)
  84.         if t and t!='exit':
  85.             correct += 1
  86.         elif t == 'exit':
  87.             return 0
  88.     correct = 0
  89.     msgbox("你答对了{0}道题,正确率是{1}%".format(correct,int(correct*10)))

  90. if __name__ == '__main__':
  91.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-20 17:00:05 | 显示全部楼层
  1. #idle版
  2. from random import *
  3. from decimal import Decimal

  4. class equation:
  5.     def __init__(self,fomula,answer):
  6.         self.fomula = fomula
  7.         self.answer  = answer
  8.     def ask(self):
  9.         print(self.fomula)
  10.         split = input("请输入你的答案(x是多少)(有多个解用 / 隔开):").split("/")
  11.         while True:
  12.             try:
  13.                 ans = [float(i.replace(" ", "")) for i in split]
  14.             except:
  15.                 split = input("输入错误,请重新输入:").split("/")
  16.             else:
  17.                 break
  18.         if ans == self.answer:
  19.             print("恭喜你,答对了!")
  20.             return True
  21.         else:
  22.             print("答错了,正确答案是"+str(self.answer))
  23.             return False

  24. def get_equation1(level):
  25.    
  26.     if level == 1:
  27.         # ax = b 类型的方程
  28.         a = randint(4,20)
  29.         x = randint(4,20)
  30.         b = a * x
  31.         fomula = str(a)+"x = "+str(b)

  32.         temp = equation(fomula,[x])
  33.         return temp

  34.     elif level == 2:
  35.         # ax = b 类型的方程,但是a x是小数
  36.         a = randint(4,20) + randint(1,9)/10
  37.         x = randint(4,20)
  38.         b = float(Decimal(str(a)) *  Decimal(str(x)))# 使用decimal模块里的Decimal函数
  39.         fomula = str(a)+"x = "+str(b)

  40.         temp = equation(fomula,[x])
  41.         return temp

  42.     elif level == 3:
  43.         # ax + b = c 类型的方程
  44.         a = randint(4,20)
  45.         x = randint(4,20)
  46.         b = randint(4,20)
  47.         c = a * x + b
  48.         fomula = str(a)+"x + "+str(b)+" = "+str(c)

  49.         temp = equation(fomula,[x])
  50.         return temp

  51.     elif level == 4:
  52.         # ax + b = c 类型的方程, a x b是小数
  53.         a = randint(4,20) + randint(1,9)/10
  54.         x = randint(4,20)
  55.         b = randint(4,20) + randint(1,9)/10
  56.         c = float(Decimal(str(a)) *  Decimal(str(x)) + Decimal(str(b))) # 使用decimal模块里的Decimal函数
  57.         fomula = str(a)+"x + "+str(b)+" = "+str(c)

  58.         temp = equation(fomula,[x])
  59.         return temp
  60.    
  61. def main():
  62.     correct = 0
  63.     for i in range(1,5):
  64.         temp = get_equation1(i)
  65.         if temp.ask():
  66.             correct += 1
  67.         temp = get_equation1(i)
  68.         if temp.ask():
  69.             correct += 1
  70.     print("你答对了{0}道题,正确率是{1}%".format(correct,int(correct*10)))

  71. if __name__ == '__main__':
  72.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-20 20:21:09 | 显示全部楼层

回帖奖励 +1 鱼币

感谢分享!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-20 20:29:47 | 显示全部楼层

回帖奖励 +1 鱼币

不知道为啥,觉得很牛逼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-20 22:02:02 | 显示全部楼层

回帖奖励 +1 鱼币

加油
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-21 00:42:59 | 显示全部楼层

回帖奖励 +1 鱼币

66666666666666666666
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-21 07:59:43 | 显示全部楼层

回帖奖励 +1 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-21 08:34:45 | 显示全部楼层

回帖奖励 +1 鱼币

感谢分享!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-21 08:36:55 | 显示全部楼层

回帖奖励 +1 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-21 09:14:23 | 显示全部楼层

回帖奖励 +1 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-21 11:34:35 | 显示全部楼层

回帖奖励 +1 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-21 15:56:23 | 显示全部楼层

回帖奖励 +1 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-21 16:10:46 | 显示全部楼层

回帖奖励 +1 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-23 22:57:31 | 显示全部楼层

回帖奖励 +1 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-26 13:38:14 | 显示全部楼层

回帖奖励 +1 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-26 20:38:29 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-26 21:26:39 | 显示全部楼层
Python如此厉害?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-27 10:07:22 | 显示全部楼层

回帖奖励 +1 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 04:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表