2012-03-02 6 views
2

私が持っている主な問題は、ユーザーがコンピュータ(ランダム関数)を使って遊んでいるときに、勝利と結びつきを得ることです。しかし、player_choiceのロックペーパーはさみのために1、2、または3のいずれかを入力するときはいつもこのエラーが発生します。Python用の私の家のはさみプログラムを見てください

Welcome to a game of paper, rock, scissors! 
Please input the correct number according 
to the object. 
Select rock(1), paper(2), or scissors(3): 2 
Computer chose ROCK . 
You chose PAPER . 
Traceback (most recent call last): 
    File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line  
114, in <module> 
    File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 
43, in main 
    File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 
106, in determine_winner 
builtins.UnboundLocalError: local variable 'win' referenced before assignment 

明らかにそのローカル変数の問題。他の解決策はありますか?

#import the library function "random" so that you can use it for computer 
#choice 
import random 

#define main 
def main(): 
    #assign win, lose, and tie variables to zero so that later it can be added 
    #and displayed 
    win = 0 
    lose = 0 
    tie = 0 

    #control loop with 'y' variable 
    play_again = 'y' 

    #start the game 
    while play_again == 'y': 
     #make a welcome message and give directions 
     print('Welcome to a game of paper, rock, scissors!') 
     print('Please input the correct number according') 
     print('to the object.') 

     #write computer and players choices as value returning functions and 
     #assign them to variables 
     computer_choice = get_computer_choice() 
     player_choice = get_player_choice() 

     #print outcome 
     print('Computer chose', computer_choice, '.') 
     print('You chose', player_choice, '.') 

     #determine who won by defining a function 
     determine_winner(computer_choice, player_choice) 

     #ask the user if they want to play again 
     play_again = input("Play again? Enter 'y' for yes or 'n' for no. ") 

    #print results 
    print('Your total wins are', win, '.') 
    print('Your total losses are', lose, '.') 
    print('Your total ties are', tie, '.') 

#define computer choice 
def get_computer_choice(): 
    #use imported random function from library 
    choice = random.randint(1,3) 

    #assign what the computer chose to rock, paper, or scissors 
    if choice == 1: 
     choice = 'ROCK' 
    elif choice == 2: 
     choice = 'PAPER' 
    else: 
     choice = 'SCISSORS' 

    #return value 
    return choice 

#define player choice 
def get_player_choice(): 
    #assign input to variable by prompting user 
    choice = int(input("Select rock(1), paper(2), or scissors(3): ")) 

    #use while function if user inputs the invalid selection 
    while choice != 1 and choice != 2 and choice != 3: 
     print('The valid numbers are rock(type in 1), paper(type in 2),') 
     print('or scissors(type in 3).') 
     choice = int(input('Enter a valid number please: ')) 

    #assign what the player chose to rock, paper, or scissors 
    if choice == 1: 
     choice = 'ROCK' 
    elif choice == 2: 
     choice = 'PAPER' 
    else: 
     choice = 'SCISSORS' 

    #return value 
    return choice 

#determine the winner by assigning the assigned variables 
def determine_winner(computer_choice, player_choice): 
    #if its a tie, add 1 to tie variable and display message 
    if computer_choice == player_choice: 
     tie += 1 
     print("It's a tie!") 

    #if its a win, add to win variable and display message 
    elif computer_choice == 'SCISSORS' and player_choice == 'ROCK': 
     win += 1 
     print('ROCK crushes SCISSORS! You win!') 
    elif computer_choice == 'PAPER' and player_choice == 'SCISSORS': 
     win += 1 
     print('SCISSORS cut PAPER! You win!') 
    elif computer_choice == 'ROCK' and player_choice == 'PAPER': 
     win += 1 
     print('PAPER covers ROCK! You win!') 

    #if it does not match any of the win criteria then add 1 to lose and 
    #display lose message 
    else: 
     lose += 1 
     print('You lose!') 
main() 

答えて

1

determine_winner()変数wintieとあなたがmain()で定義されているloseを見ることができない機能: はここに私のコードです。したがって、win +=1を実行することはできません。

通常、PythonではPythonでmain()ルーチンを使用していません(私はこれを最近使用しています - これは誰でも教えていますか?)。しかし、その内容をプログラムの場合、同じ理由でwin += 1がまだ失敗するため、動作しません。

あなたはローカル変数にdetermine_winner()wintieloseを定義し、それがその後、トップレベルのコードでは、それぞれの変数にそれらを追加し、その値を返す可能性があります。実際には、その関数内にこれらの変数をまったく必要としません。

def determine_winner(computer_choice, player_choice): 
    #if its a tie, add 1 to tie variable and display message 
    if computer_choice == player_choice: 
     print("It's a tie!") 
     return 0 

    #if its a win, add to win variable and display message 
    elif computer_choice == 'SCISSORS' and player_choice == 'ROCK': 
     print('ROCK crushes SCISSORS! You win!') 
     return 1 
    elif computer_choice == 'PAPER' and player_choice == 'SCISSORS': 
     print('SCISSORS cut PAPER! You win!') 
     return 1 
    elif computer_choice == 'ROCK' and player_choice == 'PAPER': 
     print('PAPER covers ROCK! You win!') 
     return 1 

    #if it does not match any of the win criteria then add 1 to lose and 
    #display lose message 
    else: 
     print('You lose!') 
     return -1 

およびトップレベル:例えば

result = determine_winner(computer_choice, player_choice) 
if result == -1: 
    lose += 1 
elif result == 0: 
    tie += 1 
else: 
    win += 1 
+0

ありがとうございました!私はこの問題を何時間も見つめてきました! –

+2

'main()'関数を使用していなくても、メインコードを 'if __name__ ==" __main __ ":'ステートメントの下に置くことは良い考えです。 –

+0

'if __name__ ==" __main__ ":main()'行は、実際に見た人の皆さんが実際に行っています。私は1つの目的は、 'main'実行中のすべてのコードなしでファイルをインポートできるようにすることだと思います。だから、仮想の 'randRGB()'関数などを外部スクリプトのファイルに使用することができます。 – SimonT

0

Pythonはレキシカルスコープを使用します。つまり、変数が関数内で定義されている場合、その関数外のコードは(globalとマークされていない限り)それを見ることはできません。

winと、mainの外に見える必要があるその他の変数は、globalと表示されます。より良い解決策は、コードを再構成することです。

+0

あなたはまず 'main()'の外に移動しなければなりません。そうでなければグローバルではありません。 –

+0

'global'ステートメントでは、それらはなります。これは最も洗練されたソリューションではありません。 – Taymon

+0

No.現在、モジュールの最上位レベルでは定義されていません。 'global'ステートメントはローカル変数をグローバルにしません。ローカルスコープを作成するのではなく、グローバルスコープ内の*参照された変数を探す*関数に指示します。これは、すでにグローバル変数が*存在する場合にのみ機能します。 –

1

あなたは、変数を勝ち、負けて、グローバルにすることができます。ここでは、そうではありません。 How to make them global

編集:グローバル化は良い解決策ではないと私は同意します。

+0

私はそれが良い解決策だとは思わない。まず、グローバル化しても、 'decide_winner()'の最初に 'global win'を使用しない限り、それはまだ動作しません。そして、第2のグローバル変数は決して良い考えではありません。 –

+0

結果を返すことは、グローバルを使用するよりも優れた解決策であることに同意します。私はちょうどOiugghog Jkhkhがグローバル変数の使い方を尋ねていると思った。 – bmkorkut

+0

いいえ、あなたはしないでください。 –

関連する問題