简体   繁体   English

无法在For Loop上调用Python'int'对象

[英]Python 'int' object is not callable on For Loop

I know there's been quite a few of these but after looking through them, I wasn't able to solve my issue. 我知道有很多这样的问题,但是在仔细研究之后,我无法解决我的问题。 I'm also still learning Python so a simple mix up could be happening. 我还在学习Python,因此可能会发生简单的混淆。

Thus far the error is being reached on the line with the constructed for loop. 到目前为止,使用构造的for循环在线上已达到错误。

        # down
        column = 2
        grid = [ 10, 10 ]
        while range > 0:
            # grab at the top
            cur = genes[((grid[1] - 1) * grid[0]) + column]
            prev = cur
            for a in range((grid[1] - 2), -1, -1):
                # start one below from the top
                cur = genes[(a * grid[0]) + column]
                genes[(a * grid[0]) + column] = prev
                prev = cur
            # now apply the change back to the top
            genes[((grid[1] - 1) * grid[0]) + column] = prev
            if get_fitness(genes, grid) > fitness:
                print("After Down")
                board = Board(genes, grid)
                board.print()
                print("-------------------")
                return
            range -= 1

As requested 按照要求

Traceback (most recent call last):
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA 
Projects/Tetris/tetris.py", line 144, in test_double_block
self.solveDouble([4, 4])
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA 
Projects/Tetris/tetris.py", line 167, in solveDouble
best = genetic.get_best(fnGetFitness, None, optimalFitness, geneset, 
fnDisplay, custom_mutate=fnCustomMutate, custom_create=fnCreate, maxAge=10)
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA 
Projects/Tetris/genetic.py", line 105, in get_best
maxAge, poolSize):
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA 
Projects/Tetris/genetic.py", line 131, in _get_improvement
child = new_child(parent, pindex, parents)
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA 
Projects/Tetris/genetic.py", line 102, in fnNewChild
return fnMutate(parent)
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA 
Projects/Tetris/genetic.py", line 74, in fnMutate
return _mutate_custom(parent, custom_mutate, get_fitness)
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA 
Projects/Tetris/genetic.py", line 47, in _mutate_custom
custom_mutate(childGenes)
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA 
Projects/Tetris/tetris.py", line 159, in fnCustomMutate
mutate(genes, grid, window)
File "/home/kyle/Documents/Books/GeneticAlgorithms/GA 
Projects/Tetris/tetris.py", line 65, in mutate
for a in range((grid[1] - 2), -1, -1):
TypeError: 'int' object is not callable

You defined range as a number 您将range定义为数字

while range > 0:
    ... 
    range -= 1

But you later used it as a function 但后来您将其用作功能

for a in range(...):

Rename the range integer to something else range整数重命名为其他名称

For example, you could do a backward for loop 例如,您可以执行反向for循环

for r in range(top_range, 0, -1):
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM