私と私の友人は学校でプロジェクトに取り組んでおり、いくつかの手順について助けが必要です。 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()
あなたは私たちを助けている場合我々は非常に感謝されますいずれにせよ、ありがとう!
if/elseステートメントには、 'counter1 = counter1-dicetotal'があります。あなたがそれをデバッグするとき、方程式の右側のcounter1の値は何ですか? – Lexi
これはcounter1の前の値になります。たとえば、以前の値が14で、ダイが両方とも4の場合、新しい値は6になります。カウンタ1の開始値は0ですが、コードの前の部分に割り当てられています。 –
他の回答についてのあなたのコメントに基づいて、counter1を実際に割り当てている場所を示す必要があります。私たちはあなたがしたことを確実に知ることはできません(少しの間違いが常に起こります)。あなたが提供したコードは、あなたがしなかったことを示し、あなたが私たちに見せなかったものを仮定することはできません。詳しくは、[mcve]を参照してください。 – Lexi