Python 迭代列表时if语句未传递false

Python 迭代列表时if语句未传递false,python,list,for-loop,if-statement,Python,List,For Loop,If Statement,下面的代码开始于《自动化无聊的东西》中的Tic-Tac-Toe练习,但我想让游戏检查每个回合是否有赢家 我试图把每一个赢的组合都放在它自己的列表中,并在适用于最后一步的情况下更新每个位置。接下来的想法是检查这些列表中是否有一个全部为X或全部为0,然后程序将宣布获胜 在测试时,我收到错误值error:“top-L”不在包含update\u wincon=winconditions.index(move) 上下文的周围代码: for each in winconditions:

下面的代码开始于《自动化无聊的东西》中的Tic-Tac-Toe练习,但我想让游戏检查每个回合是否有赢家

我试图把每一个赢的组合都放在它自己的列表中,并在适用于最后一步的情况下更新每个位置。接下来的想法是检查这些列表中是否有一个全部为X或全部为0,然后程序将宣布获胜

在测试时,我收到错误值error:“top-L”不在包含
update\u 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是一个列表列表,因此您无法检查其中是否有单个位置。您将遍历保存在“each”变量中的每一行可能的风,因此更改此行:

update\u wincon=winconditions.index(移动)

update\u wincon=each.index(移动)
然后,您将看到下一个错误

此实施应根据您的需要进行:

#电路板存储为字典
董事会={'top-L':'','top-M':'','top-R':'',
“mid-L:”,“mid-M:”,“mid-R:”,
“bot-L”:“bot-M”:“bot-R”:”
#将字典转换为可视板的函数
def打印板(板):
打印(线路板['top-L']+'|'+线路板['top-M']+'|'+线路板['top-R']))
打印('-+-+-')
打印(线路板['mid-L']+'|'+线路板['mid-M']+'|'+线路板['mid-R']))
打印('-+-+-')
打印(板['bot-L']+'|'+板['bot-M']+'|'+板['bot-R']))
#winconditions存储为字典,以供参考,以检查是否有赢家
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']]
转角='X'
播放=正确
游戏时:
印刷板(theBoard)
打印('Turn for'+Turn+'。移动到哪个空间?'))
move=str(输入())
如果未在板中移动。键():
打印('请输入top/mid/bot-L/M/R')
持续
棋盘[移动]=转动
对于winconditions中的每种情况:
如果(电路板[每个[0]]='X'和电路板[每个[1]]='X'和电路板[每个[2]]='X')或(每个[0]='0'和每个[1]='0'和每个[2]='0'):
打印(str(回合)+“是赢家!”)
播放=错误
如果转角='X':
回合='0'
其他:
转角='X'
问题在于“获胜条件”包含列表,而不是单个字符串。 线路应该是

update_wincon = winconditions.index(each)