修复NameError

时间:2018-02-22 19:00:23

标签: python nameerror

我正在编写一个程序,与两名玩家一起玩tic tac toe。我已经完成了基本代码(尽管效率很低),但我一直收到错误,说明没有定义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()

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

由于您的错误表明未定义播放器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)
              ....

答案 2 :(得分:0)

您只能在循环中调用play1()。您还需要play2()。但是,由于player1player2在函数中是本地定义的,因此无法解决您的问题。要解决此问题,请在global player1中添加play1(),在global player2中添加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? "))

循环:

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? "))

那时不需要适应循环。

答案 3 :(得分:0)

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

添加此行

player1 = player2 = None

在查看代码之后,我建议尝试在开始时将变量定义到全局范围。

global player1, player2
player1 = player2 = None