Python 名称未定义错误
在我正在进行的完成tic-tac-toe游戏的过程中,我尝试编写一个函数来检查是否有赢家。游戏如下所示: 董事会由以下名单组成:Python 名称未定义错误,python,function,Python,Function,在我正在进行的完成tic-tac-toe游戏的过程中,我尝试编写一个函数来检查是否有赢家。游戏如下所示: 董事会由以下名单组成: lst = ['1','2','3','4','5','6','7','8','9'] def board(): print (lst[0:3]) print (lst[3:6]) print (lst[6:]) ['1', '2', '3'] ['4', '5', '6'] ['7', '8', '9'] then this not so elegant fu
lst = ['1','2','3','4','5','6','7','8','9']
def board():
print (lst[0:3])
print (lst[3:6])
print (lst[6:])
['1', '2', '3']
['4', '5', '6']
['7', '8', '9']
then this not so elegant function to check for a winner:
def conclusion():
if all('o' == item for item in (lst[0],lst[1],lst[2])):
print('Player 2 wins!')
elif all('o' == item for item in (lst[3],lst[4],lst[5])):
print('Player 2 wins!')
elif all('o' == item for item in (lst[6],lst[7],lst[8])):
print('Player 2 wins')
elif all('o' == item for item in (lst[0],lst[3],lst[6])):
print('Player 2 wins')
elif all('o' == item for item in (lst[1],lst[4],lst[7])):
print('Player 2 wins')
elif all('o' == item for item in (lst[3],lst[6],lst[9])):
print('Player 2 wins')
elif all('o' == item for item in (lst[0],lst[4],lst[8])):
print('Player 2 wins')
elif all('o' == item for item in (lst[2],lst[4],lst[6])):
print('Player 2 wins')
elif all('x' == item for item in (lst[0],lst[1],lst[2])):
print('Player 1 wins!')
elif all('x' == item for item in (lst[3],lst[4],lst[5])):
print('Player 1 wins!')
elif all('x' == item for item in (lst[6],lst[7],lst[8])):
print('Player 1 wins')
elif all('x' == item for item in (lst[0],lst[3],lst[6])):
print('Player 1 wins')
elif all('x' == item for item in (lst[1],lst[4],lst[7])):
print('Player 1 wins')
elif all('x' == item for item in (lst[3],lst[6],lst[9])):
print('Player 1 wins')
elif all('x' == item for item in (lst[0],lst[4],lst[8])):
print('Player 1 wins')
elif all('x' == item for item in (lst[2],lst[4],lst[6])):
print('Player 1 wins')
else:
pass
然后,为游戏提供以下功能:
def move2():
conclusion()
move2=(input('Player 2: Type a number!'))
for x in lst:
if move2 == x:
lst[int(move2)-1] = 'o'
board()
move()
elif move2.isdigit() and move2 not in lst:
print('Not that number dipshit!')
break
board()
move2()
elif not move2.isdigit():
print('Not that number dipshit!')
break
board()
move2()
def move():
conclucion()
move1=(input('Player 1: Type a number!'))
for x in lst:
if move1 == x:
lst[int(move1)-1] = 'x'
board()
move2()
elif move1.isdigit() and move1 not in lst:
print('Not that number dipshit!')
board()
move()
break
elif not move1.isdigit():
print('Not that number dipshit!')
board()
move()
break
问题是,当我尝试运行它时,我不断遇到以下错误:
<ipython-input-27-8688e8182de8> in move()
1 def move():
----> 2 conclucion()
3 move1=(input('Player 1: Type a number!'))
4 for x in lst:
5 if move1 == x:
NameError: name 'conclucion' is not defined
移动中的()
1 def move():
---->2结论()
3 move1=(输入('Player 1:键入一个数字!'))
4对于lst中的x:
5如果move1==x:
NameError:未定义名称“conclucion”
有什么想法吗?我们也非常欢迎关于简化这项工作的建议 您将“结论”拼写为“结论”错误。您将“结论”拼写为“结论”错误。
def move():conclucion()
是否因为输入错误?将最后一个“c”更改为“s”,它应该可以工作。您的函数名为conclusion
,而不是conclusion
,谢谢,这太尴尬了。但现在它说‘列表索引超出范围’@Noah christorhi,欢迎来到Stack Overflow。我建议将你的后续问题作为一个新问题发布,因为这个问题已经有了与第一个问题相关的答案。(一个好的堆栈溢出问题只有一个狭隘的焦点,一次问多个不相关的问题会导致混乱的讨论。)您还应该尝试将代码缩减到说明问题的最小示例。我将在下次记下这一点,谢谢def move():conclucion()
是因为输入错误吗?将最后一个“c”更改为“s”,它应该可以工作。您的函数名为conclusion
,而不是conclusion
,谢谢,这太尴尬了。但现在它说‘列表索引超出范围’@Noah christorhi,欢迎来到Stack Overflow。我建议将你的后续问题作为一个新问题发布,因为这个问题已经有了与第一个问题相关的答案。(一个好的堆栈溢出问题只有一个狭隘的焦点,一次问多个不相关的问题会导致混乱的讨论。)您还应该尝试将代码缩减到说明问题的最小示例。我将在下次记下这一点,谢谢