无法中断python中的while循环

时间:2019-12-07 15:41:40

标签: python-3.x

我正在打井字游戏。我已经快完成了,但是最后我无法中断while循环。我已经把胜利变成了真实。但是if循环中的while语句不起作用。

board = [[1,2,3],[4,"X",6],[7,8,9]]
win = False
def DisplayBoard(board):
    def printborder():
        print("+-------+-------+-------+")
    def printline():
        print("|       |       |       |")
    def printitem(board,row):
        print("|   ",board[row][0],"   |   ",board[row][1],"   |   ",board[row][2],"   |", sep="")
    printborder()
    printline()
    printitem(board,0)
    printline()
    printborder()
    printline()
    printitem(board,1)
    printline()
    printborder()
    printline()
    printitem(board,2)
    printline()
    printborder()

def EnterMove(board):
    while True:
        move = int(input("Enter your move: "))
        for i in range(len(board)):
            for j in range(len(board[i])):
                if board[i][j]==move:
                    board[i][j]="O"
                    return None

def MakeListOfFreeFields(board):
    freefield = []
    for i in range(len(board)):
        for j in range(len(board[i])):
            for k in range(1,10):
                if board[i][j] == k:
                    freefield.insert(0,k)
    return freefield

def VictoryFor(board, sign):
    def whowin(sign):
        if sign == "O":
            print("Player wins.")
            win = True
        elif sign == "X":
            print("Computer wins.")
            win = True
    def checkvertical(board):
        for i in range(3):
            if board[0][i]==board[1][i]==board[2][i]:
                whowin(sign)
    def checkhorizontal(board):
        for i in range(3):
            if board[i][0]==board[i][1]==board[i][2]:
                whowin(sign)
    def checkdiagonal(board):
        if board[0][0]==board[1][1]==board[2][2] or board[0][2]==board[1][1]==board[2][0]:
            whowin(sign)
    checkvertical(board)
    checkhorizontal(board)
    checkdiagonal(board)

def DrawMove(board):
    from random import randrange
    while True:
        ran = randrange(1,10)
        if ran in MakeListOfFreeFields(board):
            for i in range(len(board)):
                for j in range(len(board[i])):
                    if board[i][j]==ran:
                        board[i][j]="X"
                        return None

DisplayBoard(board)
while True:
    EnterMove(board)
    DisplayBoard(board)
    VictoryFor(board, "O")
    if win==True:
        break
    DrawMove(board)
    DisplayBoard(board)
    VictoryFor(board, "X")
    if win==True:
        break
print("ended") 

fhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh fhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh fhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh fhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

1 个答案:

答案 0 :(得分:0)

whowin函数中(在VictoryFor内部),您只需创建一个本地win变量。如果要引用全局变量,则需要告诉Python:

def whowin(sign):
    global win   # <---- add this
    if sign == "O":
        print("Player wins.")
        win = True
    elif sign == "X":
        print("Computer wins.")
        win = True