Python &引用;为什么我的定义不';“我不能换字典吗?”;

Python &引用;为什么我的定义不';“我不能换字典吗?”;,python,python-3.x,Python,Python 3.x,我想为两个玩家做一个Tictatcoe游戏。当有人把它的标记放在左下角时,它就崩溃了。我不知道为什么 我检查了我的代码好几次,但没有发现问题 再次比赛后,def PLAYREATE不会更换棋盘,左下方的位置总是赢球 import sys theBoard = {'T-L': ' ', 'T-M': ' ', 'T-R': ' ', 'M-L': ' ', 'M-M': ' ', 'M-R': ' ', 'L-L': ' ', 'L-M': ' '

我想为两个玩家做一个Tictatcoe游戏。当有人把它的标记放在左下角时,它就崩溃了。我不知道为什么

我检查了我的代码好几次,但没有发现问题

再次比赛后,def PLAYREATE不会更换棋盘,左下方的位置总是赢球

import sys

theBoard = {'T-L': ' ', 'T-M': ' ', 'T-R': ' ',
            'M-L': ' ', 'M-M': ' ', 'M-R': ' ',
            'L-L': ' ', 'L-M': ' ', 'L-R': ' '}

def printBoard(board):
    print('-------')
    print('|' + board['T-L'] + '|' + board['T-M'] + '|' + board['T-R'] + '|')
    print('|' + '-+-+-' + '|')
    print('|' + board['M-L'] + '|' + board['M-M'] + '|' + board['M-R'] + '|')
    print('|' + '-+-+-' + '|')
    print('|' + board['L-L'] + '|' + board['L-M'] + '|' + board['L-R'] + '|')
    print('-------')

def playAgain() :
    print('Do you want to play again?')
    again = 0
    while True:
        print('Press: Y for rematch ; E for exit')
        again = input()
        if again == 'Y':
            turn = 'X'
            theBoard = {'T-L': ' ', 'T-M': ' ', 'T-R': ' ',
            'M-L': ' ', 'M-M': ' ', 'M-R': ' ',
            'L-L': ' ', 'L-M': ' ', 'L-R': ' '}
            break
        if again == 'y':
            turn = 'X'
            theBoard = {'T-L': ' ', 'T-M': ' ', 'T-R': ' ',
            'M-L': ' ', 'M-M': ' ', 'M-R': ' ',
            'L-L': ' ', 'L-M': ' ', 'L-R': ' '}
            break
        elif again == 'E':
            sys.exit()
        elif again == 'e':
            sys.exit()
        else:
            continue


turn = 'X'

while True:
    printBoard(theBoard)
    print('Turn for ' + turn + '. Where do you want to place your ' + turn + '?')
    move=input()
    while True:
        if ' ' in theBoard[move]:
            theBoard[move] = turn
            break
        else:
            print('This spot is taken! try something else')
            print('Where do you want to place your ' + turn + '?')
            move=input()
            continue

    if turn in (theBoard['T-L'] and theBoard['T-M'] and theBoard['T-R']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()

    if turn in (theBoard['M-L'] and theBoard['M-M'] and theBoard['M-R']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()


    if turn in (theBoard['L-L'] and theBoard['L-M'] and theBoard['L-R']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()


    if turn in (theBoard['T-L'] and theBoard['M-M'] and theBoard['L-R']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()


    if turn in (theBoard['L-L'] and theBoard['M-M'] and theBoard['T-R']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()


    if turn in (theBoard['T-L'] and theBoard['M-L'] and theBoard['L-L']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()


    if turn in (theBoard['T-M'] and theBoard['M-M'] and theBoard['L-M']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()


    if turn in (theBoard['T-R'] and theBoard['M-R'] and theBoard['L-R']):
        print('Result of the game:')
        printBoard(theBoard)
        print('Player ' + turn + ' won the game!')
        playAgain()



    if turn == 'X':
        turn = '0'
    else:
        turn = 'X'

要测试一个值是否等于其他几个值,应该使用比较链接,而不是使用
运算符,该运算符只是将值作为布尔值执行and运算

更改:

if turn in (theBoard['T-L'] and theBoard['M-L'] and theBoard['L-L']):
致:


对于所有其他
条件,也要这样做。

你应该学会调试代码,需要阅读来简化代码……好的,我改了。但要知道,如果马克被放在角落里,他就会赢。
if turn == theBoard['T-L'] == theBoard['M-L'] == theBoard['L-L']: