Python if/else 语句中的缩进错误
对于以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
if 语句的
if
一侧没有任何内容。你的代码直接跳到else
,而python期待一个块(准确地说,这是一个“缩进块”,这就是它告诉你的)至少,你需要一个带有只是一个“pass”语句,如下所示:
但是,在这种情况下,如果您真的不想在
if
端执行任何操作,那么这样做会更清楚:You don't have anything in the
if
side of your if statement. Your code skips directly to theelse
, 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:
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
必须包含一个或多个语句,例如:pass
语句是一个占位符语句,不执行任何操作。The
if
must contain one or more statements, e.g.:The
pass
statement is a placeholder statement that does nothing.