Python ';int';对象没有属性'__获取项目';当使用枚举时?

Python ';int';对象没有属性'__获取项目';当使用枚举时?,python,enumerate,Python,Enumerate,我正在尝试做一个tic-tac-toe游戏,问题是这个函数检查表,然后在选择的索引处用玩家“0”或“X”更新它 请告诉我如何修复,因为我看不出这有什么问题 table = [['1','2','3'], ['4','5','6'], ['7','8','9']] def title(): for row in table: print row def check(indx, sym): for indd, row in en

我正在尝试做一个tic-tac-toe游戏,问题是这个函数检查表,然后在选择的索引处用玩家“0”或“X”更新它

请告诉我如何修复,因为我看不出这有什么问题

table = [['1','2','3'],
         ['4','5','6'],
         ['7','8','9']]

def title():
    for row in table:
        print row

def check(indx, sym):
    for indd, row in enumerate(table):
        for ind, cell in enumerate(row):
            if cell == indx and cell != '0' or cell != 'X':
                table[indd][ind] = sym
            else:
                return False

def main():

    moves_left = 1
    while moves_left > 0:
        if moves_left == 1:
            title()
            indx = raw_input('Where do you want your nought?: ')
            new_table = check(indx, '0')
            if new_table != False:
                moves_left += 1
            else:
                print 'Invalid selection'
        elif moves_left == 2:
            title()
            indx = raw_input('Where do you want your cross?: ')
            new_table = check(indx, 'X')
            if new_table != False:
                moves_left -= 1
            else:
                print 'Invalid Selection'
        else:
            print 'it\'s a draw'

if __name__=='__main__':
    main()
我的回溯:

Traceback (most recent call last):
  File "tictac.py", line 45, in <module>
    main()
  File "tictac.py", line 28, in main
    new_table = check(indx, '0')
  File "tictac.py", line 19, in check
    table[indd[ind]] = sym
TypeError: 'int' object has no attribute '__getitem__'
回溯(最近一次呼叫最后一次):
文件“tictac.py”,第45行,在
main()
文件“tictac.py”,第28行,在main中
新建表格=检查(indx,'0')
检查文件“tictac.py”,第19行
表[indd[ind]]=sym
TypeError:“int”对象没有属性“\uuuu getitem\uuuu”
更改

table[indd[ind]] = sym # you're treating an integer like a list, which is wrong

要访问'
indd
'第行和'
ind
'第列的单元格

实际上,
table[indd[ind]]
是这样做的简写:

table.__getitem__(indd.__getitem__(ind))
&整数没有特殊的方法。

更改

table[indd[ind]] = sym # you're treating an integer like a list, which is wrong

要访问'
indd
'第行和'
ind
'第列的单元格

实际上,
table[indd[ind]]
是这样做的简写:

table.__getitem__(indd.__getitem__(ind))

&整数没有特殊的方法。

DeveloperXY已经解决了您当前的问题。还有几个。目前让您烦恼的是,您的if逻辑不正确:

if cell == indx and cell != '0' or cell != 'X':
可以使用括号,也可以学习布尔运算符的求值顺序。除此之外,这句话一开始就必须是正确的:每个单元格都不是“X”。由于您对循环中的每个单元格执行更新,因此将它们全部更改为“0”

相反,您需要设计逻辑,找到您想要更改的一个单元格(at cell==indx),然后只更改该单元格。首先,请尝试以下方法:

def check(indx, sym):
    for indd, row in enumerate(table):
        for ind, cell in enumerate(row):
            if cell == indx:
                table[indd][ind] = sym
print table  # I added this to watch what happens to the actual game state.
注意,我去掉了返回值:您返回的是NoneFalse,这在语句if new_table中具有相同的效果。 顺便说一下,请注意,如果从检查返回True,则向左移动变为2,主程序进入无限循环

现在,请注意,title函数根本不关注游戏状态:它只打印初始编号


这是否足以让您解决这个问题?

DeveloperXY已经解决了您当前的问题。还有几个。目前让您烦恼的是,您的if逻辑不正确:

if cell == indx and cell != '0' or cell != 'X':
可以使用括号,也可以学习布尔运算符的求值顺序。除此之外,这句话一开始就必须是正确的:每个单元格都不是“X”。由于您对循环中的每个单元格执行更新,因此将它们全部更改为“0”

相反,您需要设计逻辑,找到您想要更改的一个单元格(at cell==indx),然后只更改该单元格。首先,请尝试以下方法:

def check(indx, sym):
    for indd, row in enumerate(table):
        for ind, cell in enumerate(row):
            if cell == indx:
                table[indd][ind] = sym
print table  # I added this to watch what happens to the actual game state.
注意,我去掉了返回值:您返回的是NoneFalse,这在语句if new_table中具有相同的效果。 顺便说一下,请注意,如果从检查返回True,则向左移动变为2,主程序进入无限循环

现在,请注意,title函数根本不关注游戏状态:它只打印初始编号


这是否足以让您解决此问题?

谢谢您提供的信息,虽然我现在没有回溯,但这只是将表中的所有单元格设置为0。
check(indx,'0')
您总是将0传递给负责设置单元格的方法。是的,因为它是零和叉。只有当单元格不是0或x时,才会将其更改为等于indx。它不应该将所有单元格都更改为0。您能用最小的“相关”代码更新您的帖子以运行吗?好的,谢谢您提供的信息,虽然我现在没有回溯,但这只是将表中的所有单元格设置为0。
check(indx,'0'))
您总是将0传递给负责设置单元格的方法。是,因为它是零和叉。只有当单元格不是0或x时,才会将其更改为等于indx。它不应该将所有单元格都更改为0。您能用最少的“相关”代码更新您的帖子以运行吗?是的,谢谢您的帮助。是的,谢谢您的帮助。