[英]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.