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
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.)
解决方案
It's because you're assigning to score. questions and answers 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.