一个可以帮助你练习解方程的程序
本帖最后由 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:} 改进了一下
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() #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()
#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() 感谢分享! 不知道为啥,觉得很牛逼{:5_99:} 加油 66666666666666666666 {:5_108:} 感谢分享! {:10_256:} {:10_256:} {:5_106:} {:10_323:} {:10_254:} {:10_275:} {:10_257:} {:5_109:} Python如此厉害? {:10_268:}
页:
[1]
2