I am trying to make a code that rolls 2 dice and has some other requirements to look for. I have declared a variable as global but it is still giving me an error
I have also tried to put the variables as a condition in the draw() function but this will still not work
class DiceGame():
def game():
global p1total
global p1total
p1total = 0
p2total = 0
i=1
for i in range(1,5):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
dice3 = random.randint(1,6)
dice4 = random.randint(1,6)
dice5 = random.randint(1,6)
dice6 = random.randint(1,6)
if (dice1+dice2)%2 == 0:
if p1total >= 10:
p1total -+10
else:
p1total = 0
else:
p1total += 5
if dice1==dice2:
p1total += dice1+dice2+dice3
if (dice4+dice5)%2 == 0:
if p2total >= 10:
p2total -+10
else:
p2total = 0
else:
p2total += 5
if dice4==dice5:
p2total += dice4+dice5+dice6
i+=1
DiceGame.draw(p1total, p2total)
def draw(p1total, p2total):
print("Player 1 has:",p1total)
print("Player 2 has:",p2total)
if p1total==p2total:
p1dice = random.randint(1,6)
p1total+=p1dice
p2dice = random.randint(1,6)
p2total+=p2dice
DiceGame.draw()
elif p1total > p2total:
print("Player 1 wins with",p1total)
print("Player 2 lost with",p2total)
DiceGame.scores()
elif p1total < p2total:
print("Player 2 wins with",p2total)
print("Player 1 lost with",p1total)
Error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
DiceGame.game()
File "C:/Users/fredd/Desktop/dan/Dice game controlled assessment min
lines.py", line 61, in game
DiceGame.draw()
File "C:/Users/fredd/Desktop/dan/Dice game controlled assessment min
lines.py", line 63, in draw
print("Player 1 has:",p1total)
UnboundLocalError: local variable 'p1total' referenced before assignment
I just need the variables to be available to the draw() function and output the winner
This was a mistake on my part where i didnt define the variables correctly in the global statements!