Python3非类型不可下标错误
我对这个错误有严重的不满。这是家庭作业,所以我只是想找一些关于这方面的提示。。。我已经看了其他的问题,但它们没有帮助,所以希望这是好的。我是Python新手。:)我在做一个井字游戏。我确信它还有其他一些问题,但我现在最关心的是这个错误。这是我的代码:::Python3非类型不可下标错误,python,Python,我对这个错误有严重的不满。这是家庭作业,所以我只是想找一些关于这方面的提示。。。我已经看了其他的问题,但它们没有帮助,所以希望这是好的。我是Python新手。:)我在做一个井字游戏。我确信它还有其他一些问题,但我现在最关心的是这个错误。这是我的代码::: scoreList = [] """keeps track of all of the scores, moves, and indexes as tuples for each winning board""" de
scoreList = []
"""keeps track of all of the scores, moves, and indexes as tuples for each
winning board"""
def getEmptySpaces(gameBoard):
return gameBoard.count('-')
def winner(gameBoard):
case1 = gameBoard[0]+gameBoard[1]+gameBoard[2]
case2 = gameBoard[0]+gameBoard[3]+gameBoard[6]
case3 = gameBoard[0]+gameBoard[4]+gameBoard[8]
case4 = gameBoard[1]+gameBoard[4]+gameBoard[7]
case5 = gameBoard[2]+gameBoard[5]+gameBoard[8]
case6 = gameBoard[2]+gameBoard[4]+gameBoard[6]
case7 = gameBoard[3]+gameBoard[4]+gameBoard[5]
case8 = gameBoard[6]+gameBoard[7]+gameBoard[8]
if case1 == 'xxx' or case1 == 'ooo':
return True
elif case2 == 'xxx' or case2 == 'ooo':
return True
elif case3 == 'xxx' or case3 == 'ooo':
return True
elif case4 == 'xxx' or case4 == 'ooo':
return True
elif case5 == 'xxx' or case5 == 'ooo':
return True
elif case6 == 'xxx' or case6 == 'ooo':
return True
elif case7 == 'xxx' or case7 =='ooo':
return True
elif case8 == 'xxx' or case8 == 'ooo':
return True
else:
return False
def draw(gameBoard):
if getEmptySpaces(gameBoard) ==0:
if not winner(gameBoard):
return True
return False
return False
def markX(gameBoard,index):
gameBoard = gameBoard[:index] + 'x' + gameBoard[index+1:]
def markO(gameBoard,index):
gameBoard = gameBoard[:index] + 'o' + gameBoard[index+1:]
def fitnessFunction(gameBoard):
#only runs if end game or if all full
if draw(gameBoard):
return 0
else:
emptySpaces = getEmptySpaces(gameBoard)
if emptySpaces %2 == 0:
#max won
return (emptySpaces + 1) *1
else:
#max lost
return (emptySpaces + 1) *-1
def maxTurn(gameBoard):
if getEmptySpaces(gameBoard) %2 == 0:
return True
return False
def minTurn(gameBoard):
if getEmptySpaces(gameBoard) %2 != 0:
return True
return False
def miniMax(gameBoard):
if winner(gameBoard) or getEmptySpaces(gameBoard) ==0:
return fitnessFunction(gameBoard)
else:
emptyIndexes = []
count = 0
for char in str(gameBoard):
if char == "-":
emptyIndexes.append(count)
count +=1
for index in emptyIndexes:
xChild = markX(gameBoard,index)
output = miniMax(xChild) #returns score
scoreList.append((output,'x',index))
oChild = markO(gameBoard,index)
output = miniMax(oChild) #returns score
scoreList.append((output,'o',index))
return scoreList
def main():
validChars = ['x','o','-']
valid = True
gameBoard = input("Please enter your current board configuration: ")
for char in gameBoard:
if char not in validChars:
valid = False
while not valid or len(gameBoard) !=9:
print("Boards is not valid.")
gameBoard = input("Please enter your current board configuarion: ")
for char in gameBoard:
valid = True
if char not in validChars:
valid = False
else:
listOfScores = miniMax(gameBoard)
if maxTurn(gameBoard):
best = max(listOfScores, key=lambda x: x[0])
else:
best = min(listOfScores, key=lambda x: x[0])
if best[0] == 0:
print("You should mark " + best[1] + " in cell " + best[2] + ".")
print("This will lead to a tie.")
elif best[0] > 0:
print("You should mark " + best[1] + " in cell " + best[2] + ".")
print("This will lead to a win.")
else:
print("You should mark " + best[1] + " in cell " + best[2] + ".")
print("This will lead to a loss.")
main()
以下是回溯的错误::
Traceback (most recent call last):
File "C:/Users/Abby/Desktop/CS 3610/hw2/hw2Copy.py", line 134, in <module>
main()
File "C:/Users/Abby/Desktop/CS 3610/hw2/hw2Copy.py", line 117, in main
listOfScores = miniMax(gameBoard)
File "C:/Users/Abby/Desktop/CS 3610/hw2/hw2Copy.py", line 92, in miniMax
output = miniMax(xChild) #returns score
File "C:/Users/Abby/Desktop/CS 3610/hw2/hw2Copy.py", line 81, in miniMax
if winner(gameBoard) or getEmptySpaces(gameBoard) ==0:
File "C:/Users/Abby/Desktop/CS 3610/hw2/hw2Copy.py", line 16, in winner
case1 = gameBoard[0]+gameBoard[1]+gameBoard[2]
TypeError: 'NoneType' object is not subscriptable
回溯(最近一次呼叫最后一次):
文件“C:/Users/Abby/Desktop/CS 3610/hw2/hw2Copy.py”,第134行,在
main()
文件“C:/Users/Abby/Desktop/CS 3610/hw2/hw2Copy.py”,第117行,主目录
ListofScore=最小最大值(游戏板)
文件“C:/Users/Abby/Desktop/CS 3610/hw2/hw2Copy.py”,第92行,最小最大值
输出=最小最大值(xChild)#返回分数
文件“C:/Users/Abby/Desktop/CS 3610/hw2/hw2Copy.py”,第81行,最小最大值
如果赢家(游戏板)或GetEmptySpace(游戏板)=0:
文件“C:/Users/Abby/Desktop/CS 3610/hw2/hw2Copy.py”,第16行,在winner中
案例1=游戏板[0]+游戏板[1]+游戏板[2]
TypeError:“非类型”对象不可下标
我正在输入“----x----”作为我的输入。请给我一些关于如何摆脱这个错误的指导!我知道这个代码很混乱,但目前我还可以接受。谢谢大家!
编辑:::我得到了工作,但现在当我试图找到我的最大分数,我得到了以下错误:
Traceback (most recent call last):
File "C:\Users\Abby\Desktop\CS 3610\hw2\hw2Copy.py", line 134, in <module>
main()
File "C:\Users\Abby\Desktop\CS 3610\hw2\hw2Copy.py", line 120, in main
best = max(listOfScores, key=lambda x: x[0])
TypeError: unorderable types: list() > int()
回溯(最近一次呼叫最后一次):
文件“C:\Users\Abby\Desktop\CS 3610\hw2\hw2Copy.py”,第134行,在
main()
文件“C:\Users\Abby\Desktop\CS 3610\hw2\hw2Copy.py”,第120行,主目录
最佳值=最大值(列表分数,键=λx:x[0])
TypeError:无序类型:list()>int()
我以前用我自己的元组测试过这个,它成功了,返回了一个元组,所以我不确定这里出了什么问题。请帮助?在您的
markX
函数中,您应该返回gameBoard[:index]+'x'+gameBoard[index+1:][/code>而不是执行gameBoard=…
。这将而不是像您认为的那样覆盖变量。这是因为在python中,函数中定义的变量仅限于该范围
发生的情况是函数返回了None
,因为您从未将函数设置为返回任何内容。然后,您试图索引None
,这是不可能的,因为'NoneType'对象是不可下标的
markO()
函数也是如此
对于这样的东西,在我看来,最好使用类。这样,您就可以处理一个变量,例如self.gameBoard
,而不必一直在函数中重写它。在markX
函数中,您应该返回gameBoard[:index]+'x'+gameBoard[index+1:
而不是执行gameBoard=…
。这将而不是像您认为的那样覆盖变量。这是因为在python中,函数中定义的变量仅限于该范围
发生的情况是函数返回了None
,因为您从未将函数设置为返回任何内容。然后,您试图索引None
,这是不可能的,因为'NoneType'对象是不可下标的
markO()
函数也是如此
对于这样的东西,在我看来,最好使用类。这样,您就可以处理一个变量,例如self.gameBoard
,而不必一直在函数中重写它。谢谢!我使用了一个类,但是得到了相同的错误,所以我想我会这样尝试。这应该有帮助!我用一个新的令人兴奋的错误编辑了我原来的帖子!这是我第一次尝试max[,key=lambda]等,所以我可能使用不正确。如果您对此有任何反馈,我们将不胜感激。@AbigailB您真的应该为此提出一个单独的问题。。。但出现错误的原因是您似乎有嵌套列表。我真的不知道如何解决这个问题,因为这是你的程序,所以你可能不得不改成你熟悉的程序。谢谢!我使用了一个类,但是得到了相同的错误,所以我想我会这样尝试。这应该有帮助!我用一个新的令人兴奋的错误编辑了我原来的帖子!这是我第一次尝试max[,key=lambda]等,所以我可能使用不正确。如果您对此有任何反馈,我们将不胜感激。@AbigailB您真的应该为此提出一个单独的问题。。。但出现错误的原因是您似乎有嵌套列表。我真的不知道如何解决这个问题,因为这是你的程序,所以你可能不得不改成你熟悉的程序