Why does this cause an 'unresolved reference' error?

Hi All,
Steadily trying to learning Python basics…I’m trying to create a score variable here…but I get an “unresolved reference” error in the ‘choice_of_path’ function.

Can someone explain why? I refer to ‘Score’ in the top of the code?

Thanks! I assume this is something simple I don’t quite get! - Matt

name = input("What is your name? ")
score = 0

#Greeting
def greet(name):
print(“Hey there!”)
print("Great to see you, " + name)

greet(name)

def choice_of_path(question, right_answer, left_answer,):

choice1 = input(question)
if choice1 in ['R', 'r', 'right', 'Right', 'RIGHT']:
    print (right_answer)
    score += 1
elif choice1 in ['L', 'l', 'left', 'Left', 'LEFT']:
    print (left_answer)
else:
    print("You didn't choose the right thing! Try again")

choice_of_path(“You are in a field with lots of grain, which way do you go?”, “You got swallowed”, “You won the gold!”)
print(score)
choice_of_path(“You are now in a basketball stadium, which way do you go?”, “You scored a big goal”, “You lost game!”)

Hi Mavoz, or do you prefer Matt?

Can you please copy and paste the full error message you get,
starting with the line “Traceback…” all the way to the end?

Don’t post a screen shot please.

Wait, do you mean that you get this?

UnboundLocalError: local variable ‘score’ referenced before assignment

Please copy and paste error messages exactly, don’t paraphrase them.

To fix that error, put this line:

global score

as the first line of your choice_of_path function.

The reason for the error is that inside a function variables are
considered “local variables” that only exist inside the function. You
are trying to add 1 to a local variable that doesn’t exist yet! To tell
the Python interpreter that you want to use the global variable that
exists outside the function, you need that line global score at the
start of the function.

Thanks Steven…either is fine!

I think I may have figured this out…found a reference on a thread to something similar.

it seems like when you have a function any variable inside is only local…or otherwise has to be passed in.

But if I use the word global to introduce the variable it does work.

So this all seems to work now. Is my thinking right there?

name = input("What is your name? ")
score = 0

#Greeting
def greet(name):
print(“Hey there!”)
print("Great to see you, " + name)

greet(name)

def choice_of_path(question, right_answer, left_answer,):

choice1 = input(question)
global score
if choice1 in ['R', 'r', 'right', 'Right', 'RIGHT']:
    print (right_answer)
    score += 1
    print(score)
elif choice1 in ['L', 'l', 'left', 'Left', 'LEMaFT']:
    print (left_answer)
else:
    print("You didn't choose the right thing! Try again")

choice_of_path(“You are in a field with lots of grain, which way do you go?”, “You got swallowed”, “You won the gold!”)
choice_of_path(“You are now in a basketball stadium, which way do you go?”, “You scored a big goal”, “You lost game!”)

Thanks Steven…I’ll make sure next time I include full error…

I’ve now learned about gloabl versus local variables!

As I learn more…I’m finding I’m also getting a bit better at understanding what is a potential cause of the error…ie. what it means.

Thanks for your support!