【问题标题】:Fixing a NameError修复名称错误
【发布时间】:2018-02-22 19:00:22
【问题描述】:

我正在编写一个程序来与两个玩家一起玩井字游戏。我已经完成了基本代码(尽管效率很低),但我不断收到错误消息,提示未定义 player2。我已经尝试了很多方法来修复错误,但想知道你们是否有任何想法。它被第一个 player1 == player2 条件捕获。代码如下:

   def main():
    board1 = [" "," "," "]
    board2 = [" "," "," "]
    board3 = [" "," "," "]
    game(board1,board2,board3)
def play1():
    global player1
    player1 = int(input("Player 1, where would you like to move? "))
    return player1
def play2():
    global player2
    player2 = int(input("Player 2, where would you like to move? "))
    return player2
def game(brd1,brd2,brd3):
    isvalid = False
    while(not(isvalid)):
        play1()
        try:
            if player1 == player2:
                print("You can't both go to the same spot!")
        except NameError:
            if player1 == 0:
                brd1.pop(0)
                brd1.insert(0,"x")
                print(brd1)
                print(brd2)
                print(brd3)
            elif player1 == 1:
                brd1.pop(1)
                brd1.insert(1,"x")
                print(brd1)
                print(brd2)
                print(brd3)
            elif player1 == 2:
                brd1.pop(2)
                brd1.insert(2,"x")
                print(brd1)
                print(brd2)
                print(brd3)
            elif player1 == 3:
                brd2.pop(0)
                brd2.insert(0,"x")
                print(brd1)
                print(brd2)
                print(brd3)
            elif player1 == 4:
                brd2.pop(1)
                brd2.insert(1,"x")
                print(brd1)
                print(brd2)
                print(brd3)
            elif player1 == 5:
                brd2.pop(2)
                brd2.insert(2,"x")
                print(brd1)
                print(brd2)
                print(brd3)
            elif player1 == 6:
                brd3.pop(0)
                brd3.insert(0,"x")
                print(brd1)
                print(brd2)
                print(brd3)
            elif player1 == 7:
                brd3.pop(1)
                brd3.insert(1,"x")
                print(brd1)
                print(brd2)
                print(brd3)
            elif player1 == 8:
                brd3.pop(2)
                brd3.insert(2,"x")
                print(brd1)
                print(brd2)
                print(brd3)
        play2()
        if player2 == player1:
            print("You can't both go to the same spot!")
        elif player2 == 0:
            brd1.pop(0)
            brd1.insert(0,"o")
            print(brd1)
            print(brd2)
            print(brd3)
        elif player2 == 1:
            brd1.pop(1)
            brd1.insert(1,"o")
            print(brd1)
            print(brd2)
            print(brd3)
        elif player2 == 2:
            brd1.pop(2)
            brd1.insert(2,"o")
            print(brd1)
            print(brd2)
            print(brd3)
        elif player2 == 3:
            brd2.pop(0)
            brd2.insert(0,"o")
            print(brd1)
            print(brd2)
            print(brd3)
        elif player2 == 4:
            brd2.pop(1)
            brd2.insert(1,"o")
            print(brd1)
            print(brd2)
            print(brd3)
        elif player2 == 5:
            brd2.pop(2)
            brd2.insert(2,"o")
            print(brd1)
            print(brd2)
            print(brd3)
        elif player2 == 6:
            brd3.pop(0)
            brd3.insert(0,"o")
            print(brd1)
            print(brd2)
            print(brd3)
        elif player2 == 7:
            brd3.pop(1)
            brd3.insert(1,"x")
            print(brd1)
            print(brd2)
            print(brd3)
        elif player2 == 8:
            brd3.pop(2)
            brd3.insert(2,"o")
            print(brd1)
            print(brd2)
            print(brd3)
if __name__ == '__main__':
    main()

【问题讨论】:

    标签: python nameerror


    【解决方案1】:

    您只能在循环中调用 play1()。您还需要play2()。但是,这不会解决您的问题,因为 player1player2 是在函数中本地定义的。为了解决这个问题,在play1() 中添加global player1,在play2() 中添加global player2。然后代码看起来像这样

    def play1():
        global player1
        player1 = int(input("Player 1, where would you like to move? "))
    def play2():
        global player2
        player2 = int(input("Player 2, where would you like to move? "))
    

    还有循环:

    while(not(isvalid)):
        play1()
        play2() 
        if player2 == player1:
    

    解决此问题的另一种方法是将它们退回,如下所示:

    def play1():
        player1 = int(input("Player 1, where would you like to move? "))
        return player1
    def play2():
        player2 = int(input("Player 2, where would you like to move? "))
        return player2
    

    然后在你的循环中:

    while(not(isvalid)):
            player1 = play1()
            player2 = play2()
            if player2 == player1:
    

    这样可以避免全局变量。


    编辑:为了在每回合后打印棋盘,请在文件开头初始化player2 = None

    player2 = None
    def main():
    ...
    

    您仍然需要 play1()play2() 中的全局变量

    def play1():
        global player1
        player1 = int(input("Player 1, where would you like to move? "))
    def play2():
        global player2
        player2 = int(input("Player 2, where would you like to move? "))
    

    那么就不需要对循环进行任何调整了。

    【讨论】:

    • 在每个函数中添加了全局变量,但我仍然收到 NameError
    • @Alden 您是否还在 while 循环中添加了play2()?我已经扩展了我的答案以使其更清楚。
    • 我试过了,但我希望它在每回合后打印棋盘,在循环中的 play1() 之后添加 play2() 不会在每回合后打印棋盘
    • @Alden 好的,一个简单的方法是在代码顶部初始化player2 = None。 (我会输入我的答案,请参阅编辑后)
    • 好的,开始运行吧!感谢您的帮助兄弟!
    【解决方案2】:

    由于您的错误表明未定义玩家 2,请尝试添加 try except 条件以检查是否已定义玩家 2。如果它达到NameError,它可以继续游戏(打印你的棋盘),否则,通过你的 if 条件。

    while(not(isvalid)):
        play1()
        try:
        # Try going through all your conditions
             if player 1 == player 2:
             ...
        except NameError:
        # But if your player 2 is not defined, go through them all except the player 1 == player 2 clause
             if player 1 == 0:
             .....
                 print(brd1)
                  ....
    

    【讨论】:

    • 我明白你的意思。但是,我希望它在每回合后打印电路板,如果我按照您的建议进行操作,它会在输入之间交替吗?
    • 我认为在这种情况下,您可以添加 try except 条件。我会修改我的答案以考虑到这一点
    • 好的,我添加了这些条件,但是当玩家 1 输入位置时,棋盘上没有放置棋子,而是打印空棋盘
    • 我们到了那里:所以在except 下方,添加你所有的 if 条件不包括 if player 1 == player 2(见我的编辑)
    • 好的,这样就修复了错误,但是现在板子在每次转动后都不会打印
    【解决方案3】:

    从您的代码中可以清楚地看出,player1 和 player2 是在游戏方法的第二行初始化的,但它们没有定义。您可能需要定义一些值。

    添加这一行

    player1 = player2 = None
    

    此外,在查看您的代码后,我建议您尝试在开始时将变量定义为全局范围。

    global player1, player2
    player1 = player2 = None
    

    【讨论】:

      【解决方案4】:

      您的变量 -- player1player2 -- 在本地范围(play1play2)中定义,并且您尝试在该范围之外访问它们。在函数的开头,键入global player1(或player2)作为它自己的行以在全局范围内定义它。还要在所有函数之上添加player1, player2 = None, None启动它。至于为什么错误中指定了player2,那是因为它是条件中的第一个变量,Python从左到右求值。

      【讨论】:

      • 编辑了答案,包括在您的函数之上将 player1 和 player2 设置为 None
      猜你喜欢
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-07
      相关资源
      最近更新 更多