if / else语句中的Python缩进错误

时间:2012-01-26 17:15:29

标签: python syntax

代码如下:

if __name__ == '__main__':
    min_version = (2,5)
    current_version = sys.version_info
if (current_version[0] > min_version[0] or
    current_version[0] == min_version[0] and
    current_version[1] >= min_version[1]):
else:
    print "Your python interpreter is too old. Please consider upgrading."
    config = ConfigParser.ConfigParser()
    config.read('.hg/settings.ini')
    user = config.get('user','name')
    password = config.get('user','password')
    resource_name = config.get('resource','name')
    server_url = config.get('jira','server')
    main()

我收到错误:

 else:
       ^
IndentationError: expected an indented block

2 个答案:

答案 0 :(得分:6)

你在if语句的if方面没有任何内容。你的代码直接跳到else,而python期待一个块("缩进块",确切地说,这就是它告诉你的)

至少,你需要一个只有一个'通过'声明,像这样:

if condition:
    pass
else:
    # do a lot of stuff here

在这种情况下,如果你真的不想在if方面做任何事情,那么这样做会更清楚:

if not condition:
   # do all of your stuff here

答案 1 :(得分:2)

if必须包含一个或多个语句,例如:

if (current_version[0] > min_version[0] or
    current_version[0] == min_version[0] and
    current_version[1] >= min_version[1]):
    pass # <-------------------------------------ADDED
else:
    # ...

pass语句是一个什么都不做的占位符语句。