これは私が今働いているコードである場合に代入する前に参照:ローカル変数は、文
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文を書くと、代入の前に参照されている変数に問題はなく、私は何もしません。