python tkinter图片为什么要设置全局变量_为什么一些Python变量保持全局,而有些需要定义为全局变量...

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值