簡體   English   中英

IndentationError: 期望一個縮進塊,但我不知道為什么

[英]IndentationError: expected an indented block but i dont know why

我正在嘗試制作井字游戲 AI,但遇到了這個小問題。

def tic_tac_toe_AI(board):
    ...
    if board[5] == '':
        return 5
    ...

這出現在 function 中,其中 board 是字典。 但是我收到了這條消息,但我不知道為什么:

    if board[5] == '':
    ^
IndentationError: expected an indented block

好的,完整的代碼片段在這里:

def AI_easy(board, symbol):
    if symbol == 'X':
        computer = 'X'
        player = 'O'
    else:
        computer = 'O'
        player = 'X'

    possible_win_moves = test_win(board, computer)
    if len(possible_win_moves) > 0:
        print(possible_win_moves)
        return random_move(possible_win_moves)


    possible_lose_moves = test_win(board, player)
    if len(possible_lose_moves) > 0:
        return random_move(possible_lose_moves)

    possible_moves = []
    for i in [1,3,7,9]:
        if board[i] == '':
            possible_moves.append(i)
        if i == 9:

    if board[5] == '':
        return 5

    possible_moves = []
    for i in [2,4,6,8]:
        if board[i] == '':
            possible_moves.append(i)
        if i == 8:
            return random_move(possible_moves)

我使用的編輯器是 4 個空格自動替換為一個選項卡。 所以這不是問題。

在此類問題中,您應該提供整個代碼塊,以便其他人可以正確地幫助您,

發生此錯誤是因為 python 使用過程語言,如果您錯過在代碼行之間添加制表符或空格,那么您很可能會遇到此錯誤。

你應該試試這樣的在線代碼格式化程序,

https://www.tutorialspoint.com/online_python_formatter.htm

也看看這個網站,

https://www.edureka.co/blog/indentation-error-in-python

問題是if i == 9: 2 行上面的錯誤表明它來自

暫無
暫無

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

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