When I run the following function:
def checkChange():
for user in userLinks:
url = userLinks[user]
response = urllib2.urlopen(url)
html = response.read()
I get
Traceback (most recent call last):
File "InStockBot.py", line 34, in <module>
checkChange()
File "InStockBot.py", line 24, in checkChange
html = response.read()
UnboundLocalError: local variable 'response' referenced before assignment
Which makes no sense to me. I have no global var response. I expect it to work as below, normally.
>>> url="http://google.com"
>>> response = urllib2.urlopen(url)
>>> html = response.read()
>>> html
'<!doctype html>
Anyone know why I get this error?
You’re mixing tabs and spaces. Looking at the raw code you pasted:
You can see the switch in the last line. Effectively, this means that the
html = response.read()line isn’t indented as far as you think it is, meaning that ifuserLinksis empty, you’ll get:Run your code using
python -tt yourprogramname.pyto confirm this, and switch to always using four-space tabs.