簡體   English   中英

函數在while循環后不執行

[英]Function doesn't executes after while loop

我是python的新手。 我正在嘗試創建一個“井字游戲”游戲,但遇到了一些麻煩。

名為“handle turn”的函數的輸入在它應該要求用戶輸入時不執行任何操作。

基本上它發生在我添加“While”循環之后。 請告訴我我做錯了什么。 我如何在下面的代碼中執行/要求用戶輸入

def handle_turn():
        position = input("Please enter a number from 1-9: ")
        position = int(position) - 1

        board[position] = "x"
        print(display_board())

根本沒有“錯誤”,它只是不執行要求用戶輸入的 def handle_turn() 函數。 我將在下面附上完整的代碼。

#board
board = ["-", "-", "-",
         "-", "-", "-",
         "-", "-", "-"]

# if game is still going
game_is_going = True

#who win
winner = None

#current_player
current_player = "x"

#board display
def display_board():
    print(board[0] + "|" + board[1] + "|" + board[2])
    print(board[3] + "|" + board[4] + "|" + board[5])
    print(board[6] + "|" + board[7] + "|" + board[8])
    return

def play_game():
        return

print(display_board())

# action while game is still going
while game_is_going:

    #handle_turn(current_player)

    #check_if_game_over()

    #flip_player()

    if winner == "x" or winner == "o":
        print(winner + " won")


def handle_turn():
        position = input("Please enter a number from 1-9: ")
        position = int(position) - 1

        board[position] = "x"
        print(display_board())

print(handle_turn())

def check_if_game_over():
    check_if_win()
    check_if_tie()

def check_if_win():
    #check rows
    #check columns
    #check diagonals
    return

def flip_player():
    return


play_game()

在使用 while 循環之前,通過定義您在 while 中調用的函數來嘗試一下。

#board
board = ["-", "-", "-",
         "-", "-", "-",
         "-", "-", "-"]

# if game is still going
game_is_going = True

#who win
winner = None

#current_player
current_player = "x"

#board display
def display_board():
    print(board[0] + "|" + board[1] + "|" + board[2])
    print(board[3] + "|" + board[4] + "|" + board[5])
    print(board[6] + "|" + board[7] + "|" + board[8])
    return

def play_game():
        return

print(display_board())

def handle_turn():
        position = input("Please enter a number from 1-9: ")
        position = int(position) - 1

        board[position] = "x"
        print(display_board())

print(handle_turn())

def check_if_game_over():
    check_if_win()
    check_if_tie()

def check_if_win():
    #check rows
    #check columns
    #check diagonals
    return

def flip_player():
    return

# action while game is still going
while game_is_going:

    handle_turn(current_player)

    check_if_game_over()

    flip_player()

    if winner == "x" or winner == "o":
        print(winner + " won")


play_game()

首先,您在程序一開始就初始化了game-is-going = True 現在當 while 循環被執行時, game_is_going永遠不會game_is_going ,因此它變成了一個無限循環。 這解釋了為什么不要求輸入,也沒有產生錯誤。 您需要在 Python 中很好地處理縮進。

現在,您的代碼將變成這樣:-

board = ["-", "-", "-",
         "-", "-", "-",
         "-", "-", "-"]
game_is_going = True
winner = None
current_player = "x"
def display_board():
    print(board[0] + "|" + board[1] + "|" + board[2])
    print(board[3] + "|" + board[4] + "|" + board[5])
    print(board[6] + "|" + board[7] + "|" + board[8])
print(display_board())
def handle_turn():
        position = input("Please enter a number from 1-9: ")
        position = int(position) - 1

        board[position] = current_player
        print(display_board())
def play_game():
    while game_is_going:
        handle_turn()
        if check_if_game_over:
            game_is_going = False
        flip_player()
def check_if_game_over():
    check_if_win()
    check_if_tie()           # Print in the function, that the game was a tie
    # Also keep a track if all the 9 blocks have been filled and there is no space left for further moves
def check_if_win():
    # Check who is the winner and do winner = "x" or winner = "o" as required
    return
def flip_player():
    if current_player == "x":
        current_player = "o"
    else:
        current_player = "x"
play_game()
if winner == "x" or winner == "o":
    print(winner + " won")

我猜你的方法安排不正確,因為你沒有定義任何類,所以它誤導了編譯器

並且您沒有將參數傳遞給 handle_turn(): 方法

下面是你的工作代碼

board = ["-", "-", "-",
         "-", "-", "-",
         "-", "-", "-"]

# if game is still going
game_is_going = True

#who win
winner = None

#current_player
current_player = "x"

#board display
def display_board():
    print(board[0] + "|" + board[1] + "|" + board[2])
    print(board[3] + "|" + board[4] + "|" + board[5])
    print(board[6] + "|" + board[7] + "|" + board[8])
    return

def play_game():
        return

print(display_board())

def handle_turn(current_player):
        position = input("Please enter a number from 1-9: ")
        position = int(position) - 1

        board[position] = current_player

        print(display_board())

def check_if_game_over():
    check_if_win()
    # check_if_tie()


def check_if_win():
    #check rows
    #check columns
    #check diagonals
    return
# action while game is still going


def flip_player():
    return

while game_is_going:

    handle_turn(current_player)

    check_if_game_over()

    flip_player()

    if winner == "x" or winner == "o":
        print(winner + " won")



print(handle_turn())

你初始化

game_is_going = True 

在代碼的最開始,然后使用 while 循環檢查它是否為 True。 您的代碼永遠不會超過 while 循環,直到它為 false 為止,這永遠不會發生。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM