Reputation: 35
I am trying to make a simple dice roll game where two players roll a dice once for five rounds and the person with the highest score wins.
I have already tried setting the score1 variable to 0 within the function and outside of the function however this will cause the score to be reset to 0 every time.
#setting the scores as 0 before.
score1=0
score2=0
def round1():
print('Round 1, lets go')
input("Player 1 press ENTER to roll dice")
diceroll1()
input("Player 2 press ENTER to roll dice")
diceroll2()
round2()
#Round 1, calls upon dice roll functions and.
#dice roll function for player 1
def diceroll1():
import random
number = random.randint(1,6)
print("Your number is: " + str(number))
if number % 2 == 0:
number = number + 10
score1 = score1 + number
print("Your new score is: " + str(score1))
else:
number = number - 5
score1 = score1 + number
print("Your new score is: " + str(score1))
if score1 < 0:
score1 = 0
else:
score1=score1
#dice roll function for player 2
def diceroll2():
import random
number = random.randint(1,6)
print("Your number is: " + str(number))
score2
if number % 2 == 0:
number = number + 10
score2 = score2 + number
print("Your new score is: " + str(score2))
else:
number = number - 5
score2 = score2 + number
print("Your new score is: " + str(score2))
if score2 < 0:
score2 = 0
else:
score2=score2
I want it to simply add the dice value to the score but I get this error :
UnboundLocalError: local variable 'score1' referenced before assignment
Upvotes: 3
Views: 3199
Reputation: 258
Use global
. Using a global
identifier is basically like calling it public
, meaning it can be accessed from all the other parts of the code.
global score1
Upvotes: 2
Reputation: 21
You should use the global
identifier.
Some remarks about the code:
score2 = score2
. Just don't do it.I modified your code and left some interesting features there, that will help you in the future and now.
from random import randint
#setting the scores as 0 before.
score1=0
score2=0
#dice roll function for player 1
def diceroll1():
global score1
import random
number = randint(1,6)
print(f"Your number is: {str(number)}")
number += - 5 if number % 2 else 10
score1 += number
if score1 < 0:
score1 = 0
print(f"Your new score is: {str(score1)}")
#dice roll function for player 2
def diceroll2():
global score2
import random
number = randint(1,6)
print(f"Your number is: {str(number)}")
number += - 5 if number % 2 else 10
score2 += number
if score2 < 0:
score2 = 0
print(f"Your new score is: {str(score2)}")
def game_loop():
for _ in range(int(input("Raound number: "))):
print('Round 1, lets go')
input("Player 1 press ENTER to roll dice")
diceroll1()
input("Player 2 press ENTER to roll dice")
diceroll2()
print()
if __name__ == "__main__":
game_loop()
Next, try to make of those two functions one.
Upvotes: 2
Reputation: 1265
This is Just a very common question for Python. Below is my answer in somewhere else.
global
and nonlocal
are very strange things when I was a beginner.
Just think about it: why do we need them in Python?
It is because we don't need var
, let
and such similar things to declare variables.
Think about Javascript
, it is dynamic script language too and very alike to python, but it needs var
or let
or const
to declare variables.
The most important thing of declaring variables is to determine scope.
So, in Python, our variables have implicit default scope: current scope where they are defined, and if we want to change scope of some variables, we need use global
or nonlocal
explicitly .
All names on the left side of =
mean to define variables.
Before executing code of some certain scope, Python will pre-compute all local variables
, which are those on the left side of =
. This is why you got UnboundLocalError: local variable 'X' referenced before assignment
in:
def foo():
X = X + 10
So, if we look up those names not in defined current scope, just
follow the rules of scope chain: up, up, up and until built_in
.
Remember: scope of any name on the left side of =
is default current scope, and you have to assign it(bind something to it) before referring it.
Global and local scope in python - Stack Overflow
Upvotes: 0