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

发布于 2024-12-29 07:23:30 字数 715 浏览 1 评论 0原文

对于以下代码:

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

for the following code:

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()

i get error:

 else:
       ^
IndentationError: expected an indented block

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后eg是否自 2025-01-05 07:23:30

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

至少,你需要一个带有只是一个“pass”语句,如下所示:

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

但是,在这种情况下,如果您真的不想在 if 端执行任何操作,那么这样做会更清楚:

if not condition:
   # do all of your stuff here

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)

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

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
情绪少女 2025-01-05 07:23:30

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 语句是一个占位符语句,不执行任何操作。

The if must contain one or more statements, e.g.:

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

The pass statement is a placeholder statement that does nothing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文