'function'对象没有属性'__getitem__'

时间:2016-05-06 14:38:15

标签: python-2.7

这是我第一次编码。我这样做是作为选择性模块。我必须编制ai_player编程,从随机播放到获胜,我被卡住了。任何意见,将不胜感激。游戏是连接4.我不断得到“对象没有属性”错误。

import random
import time


def board():

    for i in range(0, 8, 1):
        for j in range(0, 10, 1):
            board[i][j] = 0

    return board


def move(board, valid_move):

    start_time = time.time()

    x = 0

    while x == 0:

        i = range(7, -1, -1)
        j = range(0, 10, 1)

        first_move = board[i][j]
        board[7][4] = 1

        if board[i-1][j] == 0:  #above
            first_move = [i, j]
            x = 1
            print " valid above"
            return j

        elif (board[i][j+1] == 0 and (i <= 7 and j <= 9)) or (board[i-1][j+1] == 0 and (i <= 7 and j <= 9)) or (board[i-1][j+1] == 0 and (i <= 7 and j <= 9)):  #right
            first_move = [i, (j+1)]
            x = 1
            print " valid right"
            return (j+1)

        elif board[i][j-1] == 0 or board[i-1][j-1] == 0 or board[i-1][j-1] == 0: #left
            first_move = [i, (j-1)]
            x = 1
            print " valid left"
            return (j-1)

        else:
            r = random.randint(0, 7)
            c = random.randint(0, 9)
            first_move = [r, c]
            x = 1
            print " random move"
            return c

    end_time = time.time() - start_time
    print end_time

    return first_move


File "F:/5. Fifth year/1st Semester/MPR 213 2016/Project 2016/attempts.py", line 20, in board
    board[i][j] = 0

TypeError: 'function' object has no attribute '__getitem__'

1 个答案:

答案 0 :(得分:0)

看起来您正在尝试创建名为board的多维列表。这不是你怎么做的,你实际完成的是创建了一个名为board的函数,然后你尝试索引该函数,因为它不是列表而失败。

要创建board,请使用

之类的内容
board = [[0] * 10 for i in range(0, 8)]