Python 修复名称错误

Python 修复名称错误,python,nameerror,Python,Nameerror,我正在写一个程序来和两个玩家玩井字游戏。我已经完成了基本代码(尽管效率很低),但我一直收到一个错误,说player2没有定义。我已经尝试了很多方法来修复这个错误,但是我想知道yall是否有任何想法。它在第一个player1==player2条件下被捕获。代码如下: def main(): board1 = [" "," "," "] board2 = [" "," "," "] board3 = [" "," "," "] game(board1,board

我正在写一个程序来和两个玩家玩井字游戏。我已经完成了基本代码(尽管效率很低),但我一直收到一个错误,说player2没有定义。我已经尝试了很多方法来修复这个错误,但是我想知道yall是否有任何想法。它在第一个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()

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

由于您的错误表明未定义播放器2,请尝试添加
try
条件,以检查是否定义了播放器2。如果出现
名称错误
,则可以继续游戏(打印棋盘),否则,请检查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)
              ....
您只能在循环中调用
play1()
。您还需要
play2()
。但是,这不会解决您的问题,因为
player1
player2
是在这些函数中本地定义的。要解决此问题,请在
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()中使用globals


然后不需要对循环进行自适应。

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

添加这一行

player1 = player2 = None
另外,在查看代码之后,我建议尝试在开始时将变量定义为全局范围

global player1, player2
player1 = player2 = None

我明白你的意思。但是,我希望它在每次旋转后打印电路板,如果我按照您的建议执行,它会在输入之间交替吗?我认为在这种情况下,您可以添加一个
尝试
,除了
条件。我将修改我的答案以考虑到这一点。好吧,我添加了这些条件,但是当玩家1输入一个位置时,棋盘上没有放置任何棋子,空棋盘被打印出来,而我们正在到达那里:因此在
下面除了
,添加所有不包括
如果玩家1==玩家2
(请参阅我的编辑)好的,这样修正了错误,但是现在电路板在每次旋转后都不打印,在每个函数中添加了全局变量,但是我仍然收到了NameError@Alden您是否也在while循环中添加了
play2()
?我扩展了我的答案,让它更清楚。我试过了,但我想让它在每一圈后打印电路板,在循环中的play1()之后添加play2()不会在每一圈后打印电路板turn@Alden啊,好的,一个简单的方法是在代码顶部初始化
player2=None
。(我将在我的答案中输入,请参见编辑后的内容)好的,让它开始运行!谢谢你的帮助,兄弟!编辑了答案,将player1和player2设置为“无”,并将其置于函数之上
global player1, player2
player1 = player2 = None