在if语句中赋值之前引用的局部变量

时间:2016-09-30 18:11:40

标签: python-3.x

这是我现在正在处理的代码:

def getWinner(userChoice, computerChoice):

    if userChoice == "rock" and computerChoice == "scissors":
       winner = userChoice
    elif userChoice == "paper" and computerChoice == "rock":
       winner = userChoice
    elif userChoice == "scissors" and computerChoice == "paper":
       winner = userChoice
    elif userChoice == "rock" and computerChoice == "paper":
       winner = computerChoice
    elif userChoice == "paper" and computerChoice == "scissors":
      winner = computerChoice
    elif userChoice == "scissors" and computerChoice == "rock":
      winner = computerchoice
    elif userChoice == computerChoice:
      winner = "It's a tie."
return(winner)

userChoice = input("Enter your choice:")
computerChoice = print(getComputerChoice())
winnerOfGame = getWinner(userChoice, computerChoice)
print(winnerOfGame)

我正在尝试设置摇滚,纸张,剪刀游戏,但每次尝试运行此功能时,都会返回:

Traceback (most recent call last):
   File "C:/Python34/idk 2.py", line 45, in <module>
    winnerOfGame = getWinner(userChoice, computerChoice)
   File "C:/Python34/idk 2.py", line 41, in getWinner
    return(winner)
UnboundLocalError: local variable 'winner' referenced before assignment

我尝试过分配一个全局变量,但是当我尝试修复它时,似乎没有任何工作。当我做其他if这样的语句时,我在赋值之前没有引用变量的问题,而且我没有做任何不同的事情。

2 个答案:

答案 0 :(得分:1)

您只需在函数开头声明winner即可。您不需要全局变量。如果userChoicecomputerChoice没有任何预期值,您还需要指定结果。您可能应该返回错误。

def getWinner(userChoice, computerChoice):
    winner = "" #***************
    if userChoice == "rock" and computerChoice == "scissors":
        winner = userChoice
    elif userChoice == "paper" and computerChoice == "rock":
        winner = userChoice
    elif userChoice == "scissors" and computerChoice == "paper":
        winner = userChoice
    elif userChoice == "rock" and computerChoice == "paper":
        winner = computerChoice
    elif userChoice == "paper" and computerChoice == "scissors":
       winner = computerChoice
    elif userChoice == "scissors" and computerChoice == "rock":
       winner = computerchoice
    elif userChoice == computerChoice:
       winner = "It's a tie."
    else:
       winner = "Wrong input! Try again" #************
    return winner

userChoice = input("Enter your choice:")
computerChoice = print(getComputerChoice())
winnerOfGame = getWinner(userChoice, computerChoice)
print(winnerOfGame)

答案 1 :(得分:0)

当两个参数中的任何一个具有与三个预期值不同的值(可能是一个简单的拼写错误)时,会发生错误。在这种情况下,winner永远不会被定义,所以当你到达return winner时,Python不知道你在说什么: - )

所以你应该检查这种情况。

正如我们所做的那样,您可以稍微简化一下代码。我建议使用具有三个可能值的数组。具有紧接在另一个之前的值的玩家(明智地循环)获胜:

def getWinner(userChoice, computerChoice):
    options = ["rock", "scissors", "paper"]
    # check that the two arguments have valid values:
    try:
        userChoiceId = options.index(userChoice);
    except ValueError:
        return "invalid choice: {}".format(userChoice)
    try:
        computerChoiceId = options.index(computerChoice)
    except ValueError:
        return "invalid choice: {}".format(computerChoice)
    # now there are only three outcomes:
    if userChoiceId == computerChoiceId:
        winner = "It's a tie."
    elif (userChoiceId + 1) % 3 == computerChoiceId: 
        winner = userChoice
    else:
        winner = computerChoice
    return winner