以下代码给出了错误UnboundLocalError:赋值前引用的局部变量“currentpl”:
def play(num_sq, user_choice):
drawStrip(num_sq)
if user_choice == 0:
currentpl = 1
elif user_choice == 1:
currentpl = 2
while gameover(num_sq):
if currentpl == 1:
pick = getPlayerPick(num_sq)
while not validPlay(pick, num_sq):
pick = getPlayerPick(num_sq)
makePlay(pick, player_col[currentpl])
if currentpl == 2:
pick = computerSelection(num_sq)
makePlay(pick, player_col[currentpl])
currentpl = togglePlayer(currentpl)
if currentpl == 2:
return "User"
return "Computer"
我该怎么解决这个问题?谢谢你的帮助!在
当
user_choice
不是0或1时会发生什么?在如果}行的网络。这意味着
user_choice
不是1或0,则执行currentpl = 1
或{currentpl
是“未分配的”—它确实不存在。当你到达一个像因为
currentpl
还不存在-它是未分配的。在这是不允许的-您需要考虑到
^{pr2}$user_choce
不是0或1的情况,方法是:在最后一个
elif
子句之后。在另一种方法是确保在本节之前执行的代码中
user_choice
始终等于0或等于1,在这种情况下,您可以确保在需要测试其值之前,currentpl
已被分配(存在)。在相关问题 更多 >
编程相关推荐