当我运行以下函数时:
def checkChange():
for user in userLinks:
url = userLinks[user]
response = urllib2.urlopen(url)
html = response.read()
我明白了
^{pr2}$这对我来说毫无意义。我没有全局var响应。我希望它正常工作如下。在
>>> url="http://google.com"
>>> response = urllib2.urlopen(url)
>>> html = response.read()
>>> html
'<!doctype html>
有人知道我为什么会犯这个错误吗?在
你的代码没有正确缩进。把它改成这个,它就会起作用(可能不是预期的那样,但它会起作用):
发生此错误是因为您在
for
循环中定义了response
,但是如果循环没有运行(即userLinks == []
),则永远不会设置该变量。在你在混合制表符和空格。看看你粘贴的原始代码:
你可以在最后一行看到开关。实际上,这意味着
^{pr2}$html = response.read()
行的缩进程度没有想象中的那么大,这意味着如果userLinks
为空,您将得到:使用
python -tt yourprogramname.py
运行代码以确认这一点,并切换到始终使用四个空格制表符。在相关问题 更多 >
编程相关推荐