Python: 我的 if 语句不工作但我没有得到任何错误?

Python: My if statement is not working but I don't get any error?

我正在创建一个井字游戏,但脚本中的 move_base 函数无法正常工作。我以前见过这种 if 语句,但我不知道为什么该功能不起作用。没有错误,但函数不会更新 num_word 或移动。我没有很多 python 经验,所以我不知道我做错了什么。我会为游戏添加更多功能,但我不能,因为它的基本部分不起作用。我还没有看到任何其他地方告诉我如何解决这个问题。

#tic tac toe game
#global variables
game_over = False

#game script
def game():
    #game variables

    #positions on the board
    one = '1'
    two = '2'
    three = '3'
    four = '4'
    five = '5'
    six = '6'
    seven = '7'
    eight = '8'
    nine = '9'
    positions = [one, two, three, four, five, six, seven, eight, nine]

    num_chosen = ''
    #moves in the game
    moves = 0

    #prints board
    def board():
        print(f'{one}|{two}|{three}')
        print(f'{four}|{five}|{six}')
        print(f'{seven}|{eight}|{nine}')

    #how to check who won
    def check_win_base(xo, num1, num2, num3):
        if num1 == xo and num2 == xo and num3 == xo:
            if(xo == 'x'):
                print('x player wins')
                game_over = True
            elif(xo == 'o'):
                print('o player wins')
                game_over = True
    #check_win_base applied to all numbers
    def check_win(xo):
        check_win_base(xo, one, two, three)
        check_win_base(xo, four, five, six)
        check_win_base(xo, seven, eight, nine)
        check_win_base(xo, one, four, seven)
        check_win_base(xo, two, five, eight)
        check_win_base(xo, three, six, nine)
        check_win_base(xo, one, five, nine)
        check_win_base(xo, three, five, seven)

    #checks if game is a draw
    def check_draw():
        if moves == 9:
            print('The game is a draw')
            game_over = True

    #how to pick a square
    def move_base(xo, num_word, num):    
        if num_chosen == num:
            num_word = xo
            moves += 1
    #move_base applied to all numbers
    def move(xo):
        move_base(xo, one, 1)
        move_base(xo, two, 2)
        move_base(xo, three, 3)
        move_base(xo, four, 4)
        move_base(xo, five, 5)
        move_base(xo, six, 6)
        move_base(xo, seven, 7)
        move_base(xo, eight, 8)
        move_base(xo, nine, 9)

    #all the required functions put together
    def turn(xo):
        board()
        print(f'{xo} move')
        num_chosen = int(input())
        move(xo)
        check_win(xo)
        check_draw()

    turn('x')
    turn('o')
    turn('x')
    turn('o')
    turn('x')
    turn('o')
    turn('x')
    turn('o')
    turn('x')

#checks if game is over or not
if game_over == False:
    game()
else:
    print('Game Over')

python

中关于变量的几个要点

您可以读取和更改全局变量,但不能为它们分配新值,除非您指定这是一个全局变量,因此这不起作用

def check_draw():
if moves == 9:
    print('The game is a draw')
    game_over = True

但这确实

def check_draw():
global game_over
if moves == 9:
    print('The game is a draw')
    game_over = True

string、int、float 等简单类型是不可变的,这意味着您无法更改它们,因为结果是您得到了另一个对象。在此示例中,通过更改 num_wordmoves 您实际上分配了新值,该值创建了这些变量的本地副本而不更改它的全局副本

def move_base(xo, num_word, num):    
        if num_chosen == num:
            num_word = xo
            moves += 1

这个混乱的解决方案很简单 - 使用可变对象,如 dictlist,以保留更改

这是一个有效的例子(或多或少)

# tic tac toe game
#global variables
game_over = False

# game script


def game():
    # game variables

    # positions on the board
    positions = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

    d = {
        "one": '1',
        "two": '2',
        "three": '3',
        "four": '4',
        "five": '5',
        "six": '6',
        "seven": '7',
        "eight": '8',
        "nine": '9',
        "num_chosen": "",
        "moves": 0,  # moves in the game
    }

    # prints board
    def board():
        print(f'{d["one"]}|{d["two"]}|{d["three"]}')
        print(f'{d["four"]}|{d["five"]}|{d["six"]}')
        print(f'{d["seven"]}|{d["eight"]}|{d["nine"]}')

    # how to check who won
    def check_win_base(xo, num1, num2, num3):
        global game_over
        if num1 == xo and num2 == xo and num3 == xo:
            if(xo == 'x'):
                print('x player wins')
                game_over = True
            elif(xo == 'o'):
                print('o player wins')
                game_over = True
    # check_win_base applied to all numbers

    def check_win(xo):
        check_win_base(xo, d["one"], d["two"], d["three"])
        check_win_base(xo, d["four"], d["five"], d["six"])
        check_win_base(xo, d["seven"], d["eight"], d["nine"])
        check_win_base(xo, d["one"], d["four"], d["seven"])
        check_win_base(xo, d["two"], d["five"], d["eight"])
        check_win_base(xo, d["three"], d["six"], d["nine"])
        check_win_base(xo, d["one"], d["five"], d["nine"])
        check_win_base(xo, d["three"], d["five"], d["seven"])

    # checks if game is a draw
    def check_draw():
        global game_over
        if d["moves"] == 9:
            print('The game is a draw')
            game_over = True

    # how to pick a square
    def move_base(xo, num_word, num):
        if d["num_chosen"] == num:
            d[num_word] = xo
            d["moves"] += 1
    # move_base applied to all numbers

    def move(xo):
        move_base(xo, "one", 1)
        move_base(xo, "two", 2)
        move_base(xo, "three", 3)
        move_base(xo, "four", 4)
        move_base(xo, "five", 5)
        move_base(xo, "six", 6)
        move_base(xo, "seven", 7)
        move_base(xo, "eight", 8)
        move_base(xo, "nine", 9)

    # all the required functions put together
    def turn(xo):
        board()
        print(f'{xo} move')
        d["num_chosen"] = int(input())
        move(xo)
        check_win(xo)
        check_draw()

    turn('x')
    turn('o')
    turn('x')
    turn('o')
    turn('x')
    turn('o')
    turn('x')
    turn('o')
    turn('x')


# checks if game is over or not
if game_over == False:
    game()
else:
    print('Game Over')