简体   繁体   English

if 语句在遍历列表时不传递 false

[英]if statement not passing on false while iterating through list

The code below started as the Tic Tac Toe exercise in Automate the boring stuff but I wanted the game to check if there was a winner at each turn.下面的代码开始于自动化无聊的东西中的井字游戏,但我希望游戏检查每一轮是否有赢家。

I attempted to put every win combination in it's own list and update each position where applicable to the last move made.我试图将每个获胜组合放在它自己的列表中,并更新每个 position 以适用于最后一次采取的行动。 The idea then would be to check if one of these lists had all X's or all 0's and the program would declare a winner.然后的想法是检查这些列表之一是否全是 X 或全是 0,并且程序将宣布获胜者。

While testing I received the error ValueError: 'top-L' is not in list at the line containing update_wincon = winconditions.index(move)在测试时我收到错误 ValueError: 'top-L' is not in list at the line update_wincon = winconditions.index(move)

surrounding code for context:上下文的周围代码:

    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)

In this specific section I am trying to use a for loop to iterate through the lists in winconditions to see if the move the user input is in that list, and if so, update it to the current turn ('X' or '0').在此特定部分中,我尝试使用 for 循环遍历 winconditions 中的列表,以查看用户输入的移动是否在该列表中,如果是,则将其更新为当前轮次('X' 或 '0' )。 It seems like it's not passing on the lists in winconditions that don't contain move.似乎它没有在不包含移动的 winconditions 中传递列表。 Not sure what I did wrong?不知道我做错了什么?

Complete code below.完整代码如下。

#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'

Your winconditions is a list of lists, therefore you cannot check if a single location is in there.您的 winconditions 是列表列表,因此您无法检查其中是否存在单个位置。 You iterate through each row of possible winds, which is saved in the 'each' variable, so change this line:您遍历保存在“每个”变量中的每一行可能的风,因此更改此行:

to

update_wincon = winconditions.index(move)

to

update_wincon = each.index(move)

then, you'll be left with the next error.然后,您将留下下一个错误。

This implementation should work according to your need:此实现应根据您的需要工作:

#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'

The problem is that "winning_conditions" contains lists, not a single string.问题是“winning_conditions”包含列表,而不是单个字符串。 The line should be该行应该是

update_wincon = winconditions.index(each)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM