Reputation: 3
Me and my friends are working on a project at school and we need help with some procedures. We know how to create and call one in, but for some reason it isn't working with our current code.
Whenever we try to use the procedure with certain parts of the code, this error appears:
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
Here is the code:
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()
We would be extremely grateful if you helped us in any way, thank you!
Upvotes: 0
Views: 46
Reputation: 77850
The message tells you what happened.
if dice1 == dice2:
counter1 = counter1 - dicetotal
print(my_list[1])
else:
counter1 = counter1 + dicetotal
When you start this code, counter1
does not have an assigned value. You're trying to add the dice roll to a quantity that doesn't exist. You have to give counter1
an initial value before you get here.
If you want to utilize and update an external variable with that name, you need the line
global counter1
at the top of your function.
For SO purposes, ed need you to supply all of the code that affects the problem. Telling us that there's a critical line that you didn't post is a bait-and-switch -- no fair! :-)
Upvotes: 1
Reputation: 1871
The local variable counter1
has not been defined in that function. You have to either assign it a value within the function, make it a global variable, or pass it in.
Upvotes: 0