Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy some Python variables stay global, while some require definition as global
    primarykey
    data
    text
    <p>I'm having a bit of trouble understanding why some variables are local and some are global. E.g. when I try this:</p> <pre><code>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 &lt; 3: question = randint(0, len(questions) - 1) print questions[question] for i in xrange(0, 4): print choices[question][i] response = raw_input("&gt; ") 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() </code></pre> <p>I get this error:</p> <pre><code>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 &gt; b Traceback (most recent call last): File "qex.py", line 47, in &lt;module&gt; askQ() File "qex.py", line 39, in askQ score += 1 UnboundLocalError: local variable 'score' referenced before assignment </code></pre> <p>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. <em>When I uncomment out those two lines, the script works perfectly fine -- even without me defining questions, answers, and choices as global variables.</em> 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?</p> <p>(Also, first time poster; thanks so much for all the help I've discovered while haven't needing to ask questions.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload