Python:indexer错误:列表索引超出范围仅在某些情况下发生

Python:indexer错误:列表索引超出范围仅在某些情况下发生,python,python-3.x,index-error,Python,Python 3.x,Index Error,我一直在制作一个关于tic-tac-toe游戏的程序,需要两个玩家轮流输入,比如棋盘的坐标,比如(r1,c1)->(r2,c2)->(r3,c3)->……,其中r是行,c是列,棋盘看起来像 0 1 2 3 4 5 6 7 8 我键入的程序: board=[0,1,2,3,4,5,6,7,8] def show(): print(str(board[0])+str(board[1])+str(board[2])) print(str(board[3])+str(board[4])

我一直在制作一个关于tic-tac-toe游戏的程序,需要两个玩家轮流输入,比如棋盘的坐标,比如
(r1,c1)->(r2,c2)->(r3,c3)->……
,其中r是行,c是列,棋盘看起来像

0 1 2
3 4 5
6 7 8
我键入的程序:

board=[0,1,2,3,4,5,6,7,8]
def show():
    print(str(board[0])+str(board[1])+str(board[2]))
    print(str(board[3])+str(board[4])+str(board[5]))
    print(str(board[6])+str(board[7])+str(board[8]))
def check(char,spot1,spot2,spot3):
    if board[spot1]==char and board[spot2]==char and board[spot3]==char:
        return True
def checkAll(char):
    if check(char,0,1,2):
        return True
    if check(char,3,4,5):
        return True
    if check(char,6,7,8):
        return True
    if check(char,0,3,6):
        return True
    if check(char,1,4,7):
        return True
    if check(char,2,5,8):
        return True
    if check(char,0,4,8):
        return True
    if check(char,2,4,6):
        return True
    else:
        return False
def ChangeSpotInputToAList(spotInput):
    spotafter=[]
    for i in spotInput:
        if i=="(0,0)":
            spotafter.append(0)
        if i=="(0,1)":
            spotafter.append(1)
        if i=="(0,2)":
            spotafter.append(2)
        if i=="(1,0)":
            spotafter.append(3)
        if i=="(1,1)":
            spotafter.append(4)
        if i=="(1,2)":
            spotafter.append(5)
        if i=="(2,0)":
            spotafter.append(6)
        if i=="(2,1)":
            spotafter.append(7)
        if i=="(2,2)":
            spotafter.append(8)
    return spotafter
i=0
spotInput=input().split("->")
spotafter=ChangeSpotInputToAList(spotInput)
for x in spotafter:
    show()
    if spotafter[x]%2==0:
        print("X-->",x)
        spot=x
        i+=1
        char="X"
        board[spot]=char
        if i==9:
            show()
            print("Winner: None")
            break
        if checkAll(char):
            show()
            print("Winner:",char)
            break
    if spotafter[x]%2==1:
        print("O-->",x)
        spot=x
        i+=1
        char="O"
        board[spot]=char
        if i==9:
            show()
            print("Winner: None")
            break
        if checkAll(char):
            show()
            print("Winner:",char)
            break
我尝试了一些输入,但其中一个不断出错,即
(2,2)->(0,0)->(1,1)->(0,2)->(1,0)->(0,1)
,程序显示:

012
345
678
Traceback (most recent call last):
  File "thisishardlol.py", line 55, in <module>
    if spotafter[x]%2==0:
IndexError: list index out of range
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "thisishardlol.py", line 55, in <module>
    if spotafter[x]%2==0:
IndexError: list index out of range
012
345
678
回溯(最近一次呼叫最后一次):
文件“thisishardlol.py”,第55行,在
如果spotafter[x]%2==0:
索引器:列表索引超出范围
sys.excepthook中出错:
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python3/dist packages/apport\u python\u hook.py”,第63行,apport\u excepthook
从apport.fileutils导入可能的\u打包,获取\u最近的\u崩溃
文件“/usr/lib/python3/dist-packages/apport/_-init___.py”,第5行,在
从apport.report导入报告
文件“/usr/lib/python3/dist-packages/apport/report.py”,第30行,在
导入apport.fileutils
文件“/usr/lib/python3/dist-packages/apport/fileutils.py”,第23行,在
从apport.packaging\u impl导入impl as packaging
文件“/usr/lib/python3/dist packages/apport/packaging_impl.py”,第24行,in
进口apt
文件“/usr/lib/python3/dist-packages/apt/_-init___.py”,第23行,在
进口apt_包装
ModuleNotFoundError:没有名为“apt_pkg”的模块
最初的例外是:
回溯(最近一次呼叫最后一次):
文件“thisishardlol.py”,第55行,在
如果spotafter[x]%2==0:
索引器:列表索引超出范围

有人知道我错在哪里吗?任何帮助都将不胜感激

让我们来看一个场景

spotInput=input().split("->")
spotafter=ChangeSpotInputToAList(spotInput)
如果我输入
(2,2)->(2,2)
,那么
spotafter=[8,8]

现在我输入我的for循环:

for x in spotafter:
    show()
    if spotafter[x]%2==0:
在我的第一个循环
x=8
中,我试图获取
spotafter[8]
,它不存在(
spotafter
只有2个元素)


因为我不知道你的目标,所以我帮不了你太多忙,但这就是为什么你得到了索引器。

是的,作为旁注,
对于x in y:y[x]
模式几乎总是错误的(尤其是初学者使用时)。For循环迭代项目,而不是索引。非常感谢,问题现在解决了!我将spotafter中的x更改为范围内的x(len(spotafter)):