if/else 语句中的 Python 缩进错误 [英] Python indentation error in if/else statement

查看:83
本文介绍了if/else 语句中的 Python 缩进错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下代码:

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

推荐答案

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

You don't have anything in the if side of your if statement. Your code skips directly to the else, while python was expecting a block (an "indented block", to be precise, which is what it is telling you)

至少,你需要一个只有pass"语句的块,像这样:

At the very least, you need a block with just a 'pass' statement, like this:

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

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

In that case, though, if you really don't ever want to do anything in the if side, then it would be clearer to do this:

if not condition:
   # do all of your stuff here

这篇关于if/else 语句中的 Python 缩进错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆