' INT'对象不可调用只是随机出现

时间:2016-06-28 15:05:40

标签: python runtime-error

有时这段代码运行得很好并且可以运行,但有时它会抛出int对象而不是可调用错误。我不确定为什么会这样做。

for ship in ships:
    vert_or_horz = randint(0,100) % 2
    for size in range(ship.size):
        if size == 0:
            ship.location.append((random_row(board),random_col(board)))
        else:
            # This is the horizontal placing
            if vert_or_horz != 0 and ship.size > 1:
                ship.location.append((ship.location[0][0], \
                                      ship.location[0][1] + size))
                while(ship.location[size][1] > len(board[0])) or \
                     (ship.location[size][1] < 0):
                    if ship.location[size][1] > len(board[0]):

                        ship.location[size][1]((ship.location[0][0], \
                                              ship.location[0][1] - size))
                    if ship.location[size][1] < 0:
                        ship.location[size][1]((ship.location[0][0], \
                                            ship.location[0][1] + size))

            # This is the vertical placing
            if vert_or_horz == 0 and ship.size > 1:
                ship.location.append((ship.location[0][0] + size, \
                                      ship.location[0][1]))
                while(ship.location[size][1] > len(board[0])) or \
                     (ship.location[size][1] < 0):
                    if ship.location[size][1] > len(board[0]):
                        ship.location[size][1] \
                            ((ship.location[0][0] - size, \
                              ship.location[0][1]))
                    if ship.location[size][1] < 0:
                        ship.location[size][1] \
                            ((ship.location[0][0] + size, \
                              ship.location[0][1]))

这是追溯:

Traceback (most recent call last):
  File "python", line 217, in <module>
  File "python", line 124, in create_med_game
    ship.location[size][1]((ship.location[0][0], \
TypeError: 'int' object is not callable

2 个答案:

答案 0 :(得分:0)

您正尝试在此处调用(使用..(...)语法)整数:

ship.location[size][1]((ship.location[0][0], \
                        ship.location[0][1] - size))

ship.location[size][1]是一个整数,你试图通过传入元组(ship.location[0][0], ship.location[0][1] - size)作为参数来调用它。

您的代码在多个地方犯了这个错误;其他例子包括

ship.location[size][1]((ship.location[0][0], \
                    ship.location[0][1] + size))

ship.location[size][1] \
    ((ship.location[0][0] - size, \
      ship.location[0][1]))

ship.location[size][1] \
    ((ship.location[0][0] + size, \
      ship.location[0][1]))

反斜杠仍然将物理线加入逻辑行,因此(...)调用仍然适用于整数。

然而,我不清楚你想用这个表达做什么,所以我不能在这里提供任何补救措施。

答案 1 :(得分:0)

在代码如下所示的任何地方:

ship.location[size][1]((ship.location[0][0], \
              ship.location[0][1] - size))

简单的解决方案是我需要将其分配到该位置:

ship.location[size] = ((ship.location[0][0],ship.location[0][1] - size))