我刚刚开始学习Python。有人告诉我这将是一个很好的初学者项目。但是我在这里的第32行遇到了一个无效的语法问题。我没有任何细节,但是直到我在第7行创建列表后问题才开始出现。我以前的尝试是从第12行到20行以及从25行到28行,打印得很好。但这仅是将移动放置在可见板上。我仍然需要一种方法来跟踪正方形是否被占用,以及它如何影响计算机对未占用正方形的偏见。但是看来我在这里做错了。
即使第31行和第32行之间的语法突出显示也有所不同,因此无论出现什么问题,它都在第32行触发(如下图所示)。有人可以帮助我理解问题吗?

import copy

def main():

    # Board squares list. String is either blank, O, or X. Int is adjusted bias.
    # Bool is whether or not the square is occupied, affects bias int.
    sq = [[" ", 0, False], [" ", 0, False], [" ", 0, False],
    [" ", 0, False], [" ", 0, False], [" ", 0, False],
    [" ", 0, False], [" ", 0, False], [" ", 0, False]]

    # DEBUG: Attempting to condense square attributes into a list.
    #sq_1 = " "
    #sq_2 = " "
    #sq_3 = " "
    #sq_4 = " "
    #sq_5 = " "
    #sq_6 = " "
    #sq_7 = " "
    #sq_8 = " "
    #sq_9 = " "

    # Board print, blank spaces and lines.
    board_blank = ["   |   |   " + "\n", "___|___|___" + "\n"]

    # DEBUG: Attempting to read squares from new list
    #board1 = " " + sq_1 + " | " + sq_2 + " | " + sq_3 + " " + "\n"
    #board2 = " " +  sq_4 + " | " + sq_5 + " | " + sq_6 + " " + "\n"
    #board3 = " " + sq_7 + " | " + sq_8 + " | " + sq_9 + " " + "\n"

    # Board print, updates with occupied squares after each move
    board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2[0] + " " + "\n"
    board2 = " " +  sq[3][0] + " | " + sq[4][0] + " | " + sq[5][0] + " " + "\n"
    board3 = " " + sq[6][0] + " | " + sq[7][0] + " | " + sq[8][0] + " " + "\n"

    # TEST: print board
    print_board(board1, board2, board3, board_blank)

# Board printing function
def print_board(board1, board2, board3, board_blank):
    print((" " + board_blank[0]), board1, board_blank[1], board_blank[0], board2,
    board_blank[1], board_blank[0], board3, board_blank[0])

# Acquire player's next move
def player_move():
    pass

# Calculate and execute computer's move
def comp_move():
    pass

main()
syntax-error - Python中的井字游戏计算机,语法无效-LMLPHP
syntax-error - Python中的井字游戏计算机,语法无效-LMLPHP

最佳答案

您在第31行中缺少右方括号:

board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2[0] + " " + "\n"
                                                         ^
用以下内容替换该行:
board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2][0] + " " + "\n"
                                                         ^

关于syntax-error - Python中的井字游戏计算机,语法无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64191853/

10-16 19:19