2016-09-30 5 views
-1

これは私が今働いているコードである場合に代入する前に参照:ローカル変数は、文

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文を書くと、代入の前に参照されている変数に問題はなく、私は何もしません。

答えて

0

ファンクションの冒頭にwinnerを宣言するだけで済みます。ここにはグローバル変数は必要ありません。 userChoiceまたはcomputerChoiceに期待値がない場合は、結果を何にするかを指定する必要もあります。おそらくエラーを返してください。二つの引数のいずれかが3つの期待値と異なる値を持つ場合

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) 
0

あなたが得るエラーが発生した(簡単なスペルの間違いかもしれません)。その場合、winnerは決して定義されないので、return winnerになると、Pythonはあなたの話を知りません:-)

このような状態をチェックする必要があります。

われわれは、コードを単純化することができます。私は可能な3つの値を持つ配列を使用することをお勧めします。他のプレーヤーよりすぐ前に値があるプレーヤー(ラウンドロビン方式):

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 
関連する問題