0
我试过搜索,但无法找到我的情况。奋斗'测验'python类逻辑
我有一个函数可以产生一个代数方程,一个问题和一个答案。我试图找出如何将这个传递给一个类。以下是代码的一小部分
class Question:
def __init__(self,question,answer=None,equation=None, options=None):
self.question = question
self.answer = answer
self.options = options
self.equation = equation
def questionMaker(self):
pass
def generateLinear1():
x = sympy.Symbol('x')
sign1 = random.choice([operator.add,operator.sub])
sign2 = random.choice([operator.add,operator.sub])
consts = [random.randint(1,10) for i in range(0,4)]
equation = sympy.Eq(sign1(consts[0]*x,consts[1]) ,sign2(consts[2]*x,consts[3]))
#Define answers
answer = sympy.solveset(equation)
return "Solve for x: %s" % (equation)
我的最终目标是能够通过继承创建一个变量,然后在GUI中使用。
谢谢
欢迎(因此)。我没有遵循你想要获得帮助的内容。请查看[问]和[mcve]并编辑您的问题。 – TemporalWolf
一旦你有问题文本的值,回答文本,选项和等式,你可以像这样传递给构造函数:'问题(问题,答案,等式,选项)'。它看起来像'questionMaker'应该是'@ classmethod',并且基本上做了'generateLinear1',然后创建并返回一个Question实例。例如。 'q = Question.generateLinear()'会给你一个线性方程的问题。 – 9000
一般来说,首先想想你的数据会有帮助:你需要什么样的数据和你想要的数据。之后,对算法的思考就比较容易了。 – 9000