【问题标题】:Python: My if statement is not working but I don't get any error?Python:我的 if 语句不起作用但我没有收到任何错误?
【发布时间】:2020-03-29 18:09:01
【问题描述】:

我正在创建井字游戏,但脚本中的 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')

【问题讨论】:

  • 我在代码中看到了很多问题。大多数情况下,您正在定义与外部范围中的变量同名的新变量 - 可能是因为您认为这会更改外部范围中的值,但事实并非如此。在某些情况下,您还会将值重新分配给参数变量。这也无济于事——这可能不是你所假设的。
  • 即使您在每个内部函数中修改了每个 var 的 nonlocal var,您的 move_base() 也不会起作用,因为它会尝试修改其参数 num_word,而这将不起作用。

标签: python python-3.x function if-statement tic-tac-toe


【解决方案1】:

关于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')

【讨论】:

  • 谢谢你。我不知道使用字典是解决问题的最佳方法。我也会在其他脚本中使用这种技术。
  • 请记住,在大多数情况下,使用global 的代码被视为错误代码。函数应该通过参数获取所需的一切,并使用return 返回值。
猜你喜欢
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多