Python“意外缩进”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25100948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:43:40  来源:igfitidea点击:

Python "unexpected indent"

python

提问by user2334436

I'm trying to run this python code but i keep getting error message "unexpected indent". I'm not sure whats wrong. the spacing seem to be fine. Any idea's?

我正在尝试运行此 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  

采纳答案by Padraic Cunningham

services = ['Service1']is not in line with your for loop.

services = ['Service1']不符合你的 for 循环。

services = ['Service1']  
 for service in services: <-- unexpected indentation 

Just line up your code as below.

只需按如下方式排列您的代码。

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

Your code is also python 2syntax not python 3

您的代码也是python 2语法不python 3