从不同的函数获取给定条件的属性

时间:2017-05-15 07:42:05

标签: python iteration

我创建了一个项目,使用数字和数学运算符的组合来创建方程,以获得预期的数字。

GIVEN:

time to create the equation, set of number, set of operator, a goal

功能包括:

  • 创建数字序列和运算符

  • 评估序列(以获得结果)

  • 改变序列

  • 获得最佳序列,其中sequence = goal

  • 的结果

问题: 我需要sequencetime result等于goal

以下是我上次试用的完整脚本:

self.goal = 22
self.time = 4
self.sequence = sequence
self.BestSequence = []

def step(self):
    while self.time > 0:
        self.time -= 1
        self.createSeqeuence ()  #create sequence
        self.evaluate()          #evaluate sequence
        self.allResult.append(self.evaluate()) #make list of result within time
        self.mutateSequence()    #modify sequence
    self.getBestSequence() #after time = 0, get the best sequence from allResult where result = goal
    print(self.result, self.BestSequence,self.time) #show

def createSeqeuence (self)
def evaluate(self)
def changeSequence(self)

结果:

time     sequence                  result
1      ['12','+','8']                20
2      ['10','+','8']                18
3      ['5','*','3']                 15
4      ['10','*','2','+','2']        22   

问题>>>>

def getBestSequence(self):
    if self.leadTime > 0:
        for j in self.allResult:
            self.BestSequence = [k for k in self.sequence if j == self.goal]
            return 

我希望得到:[22, ['10','*','2','+','2'], 4 ],但我得到了:[10 None 0]注意,这些功能总是得不到时间内结果=目标的序列。因此def getBestSequence ()可能会返回[]

请咨询

0 个答案:

没有答案