tommyyu 发表于 2022-3-20 14:53:57

一个可以帮助你练习解方程的程序

本帖最后由 tommyyu 于 2022-3-20 19:27 编辑

from random import *
from decimal import Decimal
correct = 0

class equation:
    def __init__(self,fomula,answer):
      self.fomula = fomula
      self.answer= answer
    def ask(self):
      print(self.fomula)
      ans =
      if ans == self.answer:
            print("恭喜你,答对了!")
            return True
      else:
            print("答错了,正确答案是"+str(self.answer))
            return False

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,)
      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,)
      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,)
      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,)
      return temp
   
def main():
    for i in range(1,5):
      temp = get_equation1(i)
      if temp.ask():
            correct += 1
      temp = get_equation1(i)
      if temp.ask():
            correct += 1
    print("你答对了{0}道题,正确率是{1}%".format(correct,int(correct*10)))

if __name__ == '__main__':
    main()
注:1.该程序只能练习一元一次方程 (也许下次可以改进{:10_256:})
      2.该程序不能挑选难度(也许下次可以改进{:10_256:})
      3.欢迎大家来挑错{:10_298:}

ckblt 发表于 2022-3-20 15:31:19

改进了一下
from random import *
from decimal import Decimal

correct = 0


class equation:
    def __init__(self, formula, answer):
      self.formula = formula
      self.answer = answer

    def ask(self):
      print(self.formula)
      split = input("请输入你的答案(x是多少)(有多个解用 / 隔开):").split("/")
      while True:
            try:
                ans =
            except:
                split = input("输入错误,请重新输入:").split("/")
            else:
                break
      if ans == self.answer:
            print("恭喜你,答对了!")
            return True
      else:
            print("答错了,正确答案是: ", end="")
            print(*self.answer, sep=", ")
            return False


def get_equation1(level):

    if level == 1:
      # ax = b 类型的方程
      a = randint(4, 20)
      x = randint(4, 20)
      b = a * x
      formula = str(a) + "x = " + str(b)

      temp = equation(formula, )
      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函数
      formula = str(a) + "x = " + str(b)

      temp = equation(formula, )
      return temp

    elif level == 3:
      # ax + b = c 类型的方程
      a = randint(4, 20)
      x = randint(4, 20)
      b = randint(4, 20)
      c = a * x + b
      formula = str(a) + "x + " + str(b) + " = " + str(c)

      temp = equation(formula, )
      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函数
      formula = str(a) + "x + " + str(b) + " = " + str(c)

      temp = equation(formula, )
      return temp


def main():
    global correct

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


if __name__ == "__main__":
    main()

tommyyu 发表于 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 =
            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,)
      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,)
      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,)
      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,)
      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()

tommyyu 发表于 2022-3-20 17:00:05

#idle版
from random import *
from decimal import Decimal

class equation:
    def __init__(self,fomula,answer):
      self.fomula = fomula
      self.answer= answer
    def ask(self):
      print(self.fomula)
      split = input("请输入你的答案(x是多少)(有多个解用 / 隔开):").split("/")
      while True:
            try:
                ans =
            except:
                split = input("输入错误,请重新输入:").split("/")
            else:
                break
      if ans == self.answer:
            print("恭喜你,答对了!")
            return True
      else:
            print("答错了,正确答案是"+str(self.answer))
            return False

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,)
      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,)
      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,)
      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,)
      return temp
   
def main():
    correct = 0
    for i in range(1,5):
      temp = get_equation1(i)
      if temp.ask():
            correct += 1
      temp = get_equation1(i)
      if temp.ask():
            correct += 1
    print("你答对了{0}道题,正确率是{1}%".format(correct,int(correct*10)))

if __name__ == '__main__':
    main()

hornwong 发表于 2022-3-20 20:21:09

感谢分享!

C丁洞杀O 发表于 2022-3-20 20:29:47

不知道为啥,觉得很牛逼{:5_99:}

tjweiyanmin 发表于 2022-3-20 22:02:02

加油

amazed 发表于 2022-3-21 00:42:59

66666666666666666666

Passepartout 发表于 2022-3-21 07:59:43

{:5_108:}

1molHF 发表于 2022-3-21 08:34:45

感谢分享!

阿萨德按时 发表于 2022-3-21 08:36:55

{:10_256:}

心驰神往 发表于 2022-3-21 09:14:23

{:10_256:}

伽羅~ 发表于 2022-3-21 11:34:35

{:5_106:}

a1372245671 发表于 2022-3-21 15:56:23

{:10_323:}

1271425661 发表于 2022-3-21 16:10:46

{:10_254:}

myqf123 发表于 2022-3-23 22:57:31

{:10_275:}

超级无敌磊果果 发表于 2022-3-26 13:38:14

{:10_257:}

tjweiyanmin 发表于 2022-3-26 20:38:29

{:5_109:}

C丁洞杀O 发表于 2022-3-26 21:26:39

Python如此厉害?

零的零次方 发表于 2022-3-27 10:07:22

{:10_268:}
页: [1] 2
查看完整版本: 一个可以帮助你练习解方程的程序