我试图运行此python代码,但我不断收到错误消息"意外缩进"。我不确定什么是错的。间距似乎很好。有什么想法吗?
services = ['Service1']
for service in services:
try:
print service + '\t',
if 'notavailable' not in requests.get('some website' + service + '&username=' + username, headers={'X-Requested-With': 'XMLHttpRequest'}).text:
print 'Available'
else:
print ''
except Exception as e:
print e
答案 0 :(得分:4)
services = ['Service1']
与你的for循环不一致。
services = ['Service1']
for service in services: <-- unexpected indentation
如下所示排列代码。
services = ['Service1'] # need to line up with loop
for service in services:
try:
print service + '\t',
if 'notavailable' not in requests.get('some website' + service + '&username=' + username, headers={'X-Requested-With': 'XMLHttpRequest'}).text:
print 'Available'
else:
print ''
except Exception as e:
print e
您的代码也是python 2
语法而不是python 3