2017-06-23 2 views
0

私と私の友人は学校でプロジェクトに取り組んでおり、いくつかの手順について助けが必要です。 1つを作成して呼び出す方法はわかっていますが、何らかの理由で現在のコードでは機能しません。Python 3.6/3.2の手順のヘルプが必要

私たちは、コードの特定の部分で手順を使用しようとするたびに、このエラーが表示されます。ここでは

Traceback (most recent call last): 
    File "F:\board game edited.py", line 78, in <module> 
    P1() 
    File "F:\board game edited.py", line 53, in P1 
    counter1 = counter1 + dicetotal 
UnboundLocalError: local variable 'counter1' referenced before assignment 

はコードです:

import random 

counter1 = 0 
counter2 = 0 

print("***********************************BOARD GAME**********************************") 
print(" ") 
print("***********************************GAME RULES**********************************") 
print(" ") 
print("----------------------------Game is for 2 players only-------------------------") 
print(" ") 
print(">The game board is a 7x7 board going up to 49") 
print(" ") 
print("43 44 45 46 47 48 49") 
print("42 41 40 39 38 37 36") 
print("29 30 31 32 33 34 35") 
print("28 27 26 25 24 23 22") 
print("15 16 17 18 19 20 21") 
print("14 13 12 11 10 9 8 ") 
print("1 2 3 4 5 6 7 ") 
print(" ") 
print(">The objective is to be the first player to reach space 49") 
print(" ") 
print(">There are 2 die, if you roll the same number twice, you will go back 
the number of spaces you rolled") 
print(" ") 
print(">If you land on spaces 27, 31 and 47, you will go back to space 24") 
print(" ") 
print(">Press ENTER to play") 
input() 

print("**********************************START 
GAME***********************************") 
input() 

print("Starting positions for both players = 0") 
print(" ") 

with open("Game Messages.txt","r") as infile: 
    data = infile.read() 
    my_list = data.splitlines() 

def P1(): 
    print(my_list[0]) 
    dice1 = random.randint(1,6) 
    print("dice 1 =",dice1) 
    dice2 = random.randint(1,6) 
    print("dice 2 =",dice2) 
    dicetotal = dice1 + dice2 
    print("dice total =",dicetotal) 
    if dice1 == dice2: 
     counter1 = counter1 - dicetotal 
     print(my_list[1]) 
    else: 
     counter1 = counter1 + dicetotal 
    if counter1 >= 49: 
     print("P1 space = 49") 
    if counter1 <= 0: 
     print("P1 space = 0") 
    if counter1 <=49: 
     print("P1 space =",counter1) 
    if counter1 == 47: 
     counter1 = 24 
     print(my_list[2]) 
    if counter1 == 27: 
     counter1 = 24 
     print(my_list[3]) 
    if counter1 == 31: 
     counter1 = 24 
     print(my_list[4]) 
    if counter1 >= 49: 
     print(" ") 
     print(my_list[5]) 
     print(" ") 
     print("Press ENTER to exit the game") 
     input() 
     exit() 
    input() 

def P2(): 
    print(my_list[6]) 
    dice1 = random.randint(1,6) 
    print("dice 1 =",dice1) 
    dice2 = random.randint(1,6) 
    print("dice 2 =",dice2) 
    dicetotal = dice1 + dice2 
    print("dice total =",dicetotal) 
    if dice1 == dice2: 
     counter2 = counter2 - dicetotal 
     print(my_list[7]) 
    else: 
     counter2 = counter2 + dicetotal 
    if counter2 >= 49: 
     print("P2 space = 49") 
    if counter1 <= 0: 
     print("P2 space = 0") 
    if counter1 <= 49: 
     print("P2 space =",counter2) 
    if counter2 == 47: 
     counter2 = 24 
     print(my_list[8]) 
    if counter2 == 27: 
     counter2 = 24 
     print(my_list[9]) 
    if counter2 == 31: 
     counter2 = 24 
     print(my_list[10]) 
    if counter2 >= 49: 
     print(" ") 
     print(my_list[11]) 
     print(" ") 
     print("Press ENTER to exit the game") 
     input() 
     exit() 
    input() 

P1() 
P2() 

あなたは私たちを助けている場合我々は非常に感謝されますいずれにせよ、ありがとう!

+0

if/elseステートメントには、 'counter1 = counter1-dicetotal'があります。あなたがそれをデバッグするとき、方程式の右側のcounter1の値は何ですか? – Lexi

+0

これはcounter1の前の値になります。たとえば、以前の値が14で、ダイが両方とも4の場合、新しい値は6になります。カウンタ1の開始値は0ですが、コードの前の部分に割り当てられています。 –

+3

他の回答についてのあなたのコメントに基づいて、counter1を実際に割り当てている場所を示す必要があります。私たちはあなたがしたことを確実に知ることはできません(少しの間違いが常に起こります)。あなたが提供したコードは、あなたがしなかったことを示し、あなたが私たちに見せなかったものを仮定することはできません。詳しくは、[mcve]を参照してください。 – Lexi

答えて

0

ローカル変数counter1は、その関数で定義されていません。関数内に値を代入するか、グローバル変数にするか、それを渡す必要があります。

+0

手続きの前に、表示されていないコードの前の部分にそれを割り当てました。私はそれを内部に割り当てる必要がありますか? –

+0

はい、最初に実行されるコードだけでなく、関数の上に宣言するか(関数を 'P1(counter1)'に変更する) – yinnonsanders

1

メッセージには何が起こったかが示されます。

if dice1 == dice2: 
    counter1 = counter1 - dicetotal 
    print(my_list[1]) 
else: 
    counter1 = counter1 + dicetotal 

あなたはこのコードを起動すると、counter1は、割り当てられた値を持っていません。存在しない量にサイコロロールを追加しようとしています。あなたがここに来る前にcounter1に初期値を与える必要があります。あなたがを利用してその名前の外部変数を更新する場合

は、あなたはあなたの関数の先頭に行

global counter1 

を必要としています。

SOの目的のために、すべての問題に影響するコードを提供する必要があります。投稿していないクリティカルな行があると私たちに伝えることは、餌とスイッチです - 公正ではありません! :-)

+0

これはコードの一部ですが、以前はcounter1の値を割り当てていました。とにかくありがとうございました! –

+1

あなたは機能全体を投稿しましたか?私はその範囲の一番上まで見ることができ、あなたは 'counter1'を初期化していません。その変数を使用しているので、ローカル変数です。関数の外で初期化したものは、無関係の変数です。 – Prune

+0

私はグローバルなcounter1行を使用し、それは働いた!どうもありがとうございます!! –