修复 NameError
Fixing a NameError
我正在编写一个程序来与两个玩家一起玩井字游戏。我已经完成了基本代码(尽管效率很低),但我一直收到一条错误消息,提示未定义 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()
您的变量 - player1
和 player2
- 在本地范围(play1
和 play2
)中定义,您尝试在该范围之外访问它们.在函数的开头,键入 global player1
(或 player2
)作为它自己的行以在全局范围内定义它。还要在所有函数之上添加 player1, player2 = None, None
以 启动 它。至于为什么在错误中指定player2
,那是因为它是条件中的第一个变量,而Python评估left-to-right。
由于您的错误提示玩家 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)
....
您只在循环中调用 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:
解决此问题的另一种方法是 return 它们,如下所示:
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? "))
那么不需要对循环进行适配。
从您的代码中可以清楚地看出,player1 和 player2 在 game 方法的第二行进行了初始化,但未定义。您可能需要定义一些值。
添加这一行
player1 = player2 = None
此外,在查看您的代码后,我建议尝试在开始时将变量定义到全局范围。
global player1, player2
player1 = player2 = None
我正在编写一个程序来与两个玩家一起玩井字游戏。我已经完成了基本代码(尽管效率很低),但我一直收到一条错误消息,提示未定义 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()
您的变量 - player1
和 player2
- 在本地范围(play1
和 play2
)中定义,您尝试在该范围之外访问它们.在函数的开头,键入 global player1
(或 player2
)作为它自己的行以在全局范围内定义它。还要在所有函数之上添加 player1, player2 = None, None
以 启动 它。至于为什么在错误中指定player2
,那是因为它是条件中的第一个变量,而Python评估left-to-right。
由于您的错误提示玩家 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)
....
您只在循环中调用 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:
解决此问题的另一种方法是 return 它们,如下所示:
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? "))
那么不需要对循环进行适配。
从您的代码中可以清楚地看出,player1 和 player2 在 game 方法的第二行进行了初始化,但未定义。您可能需要定义一些值。
添加这一行
player1 = player2 = None
此外,在查看您的代码后,我建议尝试在开始时将变量定义到全局范围。
global player1, player2
player1 = player2 = None