Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why some Python variables stay global, while some require definition as global

I'm having a bit of trouble understanding why some variables are local and some are global. E.g. when I try this:

from random import randint

score = 0

choice_index_map = {"a": 0, "b": 1, "c": 2, "d": 3}

questions = [
    "What is the answer for this sample question?",
    "Answers where 1 is a, 2 is b, etc.",
    "Another sample question; answer is d."
]

choices = [
    ["a) choice 1", "b) choice 2", "c) choice 3", "d) choice 4"],
    ["a) choice 1", "b) choice 2", "c) choice 3", "d) choice 4"],
    ["a) choice 1", "b) choice 2", "c) choice 3", "d) choice 4"]
]

answers = [
    "a",
    "b",
    "d"
]

assert len(questions) == len(choices), "You haven't properly set up your question-choices."
assert len(questions) == len(answers), "You haven't properly set up your question-answers."

def askQ():
    # global score
    # while score < 3:
        question = randint(0, len(questions) - 1)

        print questions[question]
        for i in xrange(0, 4):
            print choices[question][i]
        response = raw_input("> ")

        if response == answers[question]:
            score += 1
            print "That's correct, the answer is %s." % choices[question][choice_index_map[response]]
            # e.g. choices[1][2]
        else:
            score -= 1
            print "No, I'm sorry -- the answer is %s." % choices[question][choice_index_map[answers[question]]]
        print score

askQ()

I get this error:

Macintosh-346:gameAttempt Prasanna$ python qex.py 
Answers where 1 is a, 2 is b, etc.
a) choice 1
b) choice 2
c) choice 3
d) choice 4
> b
Traceback (most recent call last):
  File "qex.py", line 47, in <module>
    askQ()
  File "qex.py", line 39, in askQ
    score += 1
UnboundLocalError: local variable 'score' referenced before assignment

Now, it totally makes sense to me why it's throwing me an error on score. I didn't set it globally (I commented that part out intentionally to show this). And I am specifically not using the while clause to get it to move on (otherwise it won't even enter the clause). What confuses me is why it doesn't give me the same error for questions, choices, and answers. When I uncomment out those two lines, the script works perfectly fine -- even without me defining questions, answers, and choices as global variables. Why is that? This is the one thing I haven't been able to discover from searching other questions -- here it seems that Python is being inconsistent. Does it have to do with me using lists as the other variables? Why is that?

(Also, first time poster; thanks so much for all the help I've discovered while haven't needing to ask questions.)

like image 216
pswaminathan Avatar asked Nov 13 '12 03:11

pswaminathan


Video Answer


3 Answers

It's because you're assigning to score. The questions and answers variables are only being read, not written to.

When you assign to a variable, that name has the scope of the current method, class, etc. that you are in. When you try to get the value of a variable, it first tries the current scope, and then outer scopes, until it finds a match.

like image 190
Jonathon Reinhart Avatar answered Sep 17 '22 01:09

Jonathon Reinhart


This makes total sense if you think that the global is a parser directive

When you are doing

score += 1 

is translated to

score = score + 1

when the parser gets to 'score =' it causes the interpreter not to search outside the local space.

http://docs.python.org/2/reference/simple_stmts.html#global

like image 38
Juan Carlos Moreno Avatar answered Sep 17 '22 01:09

Juan Carlos Moreno


What happens is, Python will check for your variables in the local scope before it checks the global scope. So, when it comes to questions and answers, they are never set in the local scope, so Python moves on to the global scope, where they are found. But for score, Python sees that you make an assignment (score += 1 or score -= 1) and locks in to the local scope. But when you mention score in these statements, it does not yet exist, so Python throws an exception.

like image 28
pydsigner Avatar answered Sep 17 '22 01:09

pydsigner