[英]if statement not passing on false while iterating through list
下面的代码开始于自动化无聊的东西中的井字游戏,但我希望游戏检查每一轮是否有赢家。
我试图将每个获胜组合放在它自己的列表中,并更新每个 position 以适用于最后一次采取的行动。 然后的想法是检查这些列表之一是否全是 X 或全是 0,并且程序将宣布获胜者。
在测试时我收到错误 ValueError: 'top-L' is not in list at the line update_wincon = winconditions.index(move)
上下文的周围代码:
for each in winconditions:
if move in each:
update_wincon = winconditions.index(move)
winconditions[update_wincon] = turn
if each[1] == 'X' and each[2] =='X' and each[3] == 'X' or each[1] == '0' and each[2] =='0' and each[3] == '0':
print(str(turn) + ' is the winner!')
break
print(winconditions)
在此特定部分中,我尝试使用 for 循环遍历 winconditions 中的列表,以查看用户输入的移动是否在该列表中,如果是,则将其更新为当前轮次('X' 或 '0' )。 似乎它没有在不包含移动的 winconditions 中传递列表。 不知道我做错了什么?
完整代码如下。
#board is stored as a dictionary
theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
'bot-L': ' ', 'bot-M': ' ', 'bot-R': ' '}
#function to convert dictionary into visual board
def printBoard(board):
print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])
print('-+-+-')
print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])
print('-+-+-')
print(board['bot-L'] + '|' + board['bot-M'] + '|' + board['bot-R'])
#winconditions stored as dictionary for reference to check if there is a winner
winconditions = [['top-L', 'top-M', 'top-R'], ['mid-L', 'mid-M', 'mid-R'], ['bot-L', 'bot-M', 'bot-R'],
['top-L', 'mid-L', 'bot-L'], ['top-M', 'mid-M', 'bot-M'], ['top-R', 'mid-R', 'bot-R'],
['top-L', 'mid-M', 'bot-R'], ['top-R', 'mid-M', 'bot-L']]
turn = 'X'
while True:
printBoard(theBoard)
print('Turn for ' + turn + '. Move on which space?')
move = str(input())
if move not in theBoard:
print('please enter top/mid/bot-L/M/R')
continue
theBoard[move] = turn
for each in winconditions:
if move in each:
update_wincon = winconditions.index(move)
winconditions[update_wincon] = turn
if each[1] == 'X' and each[2] =='X' and each[3] == 'X' or each[1] == '0' and each[2] =='0' and each[3] == '0':
print(str(turn) + ' is the winner!')
break
print(winconditions)
if turn == 'X':
turn = '0'
else:
turn = 'X'
您的 winconditions 是列表列表,因此您无法检查其中是否存在单个位置。 您遍历保存在“每个”变量中的每一行可能的风,因此更改此行:
至
update_wincon = winconditions.index(move)
至
update_wincon = each.index(move)
然后,您将留下下一个错误。
此实现应根据您的需要工作:
#board is stored as a dictionary
theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
'bot-L': ' ', 'bot-M': ' ', 'bot-R': ' '}
#function to convert dictionary into visual board
def printBoard(board):
print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])
print('-+-+-')
print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])
print('-+-+-')
print(board['bot-L'] + '|' + board['bot-M'] + '|' + board['bot-R'])
#winconditions stored as dictionary for reference to check if there is a winner
winconditions = [['top-L', 'top-M', 'top-R'], ['mid-L', 'mid-M', 'mid-R'], ['bot-L', 'bot-M', 'bot-R'],
['top-L', 'mid-L', 'bot-L'], ['top-M', 'mid-M', 'bot-M'], ['top-R', 'mid-R', 'bot-R'],
['top-L', 'mid-M', 'bot-R'], ['top-R', 'mid-M', 'bot-L']]
turn = 'X'
play = True
while play:
printBoard(theBoard)
print('Turn for ' + turn + '. Move on which space?')
move = str(input())
if move not in theBoard.keys():
print('please enter top/mid/bot-L/M/R')
continue
theBoard[move] = turn
for each in winconditions:
if (theBoard[each[0]] == 'X' and theBoard[each[1]] =='X' and theBoard[each[2]] == 'X') or (each[0] == '0' and each[1] =='0' and each[2] == '0'):
print(str(turn) + ' is the winner!')
play = False
if turn == 'X':
turn = '0'
else:
turn = 'X'
问题是“winning_conditions”包含列表,而不是单个字符串。 该行应该是
update_wincon = winconditions.index(each)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.