网站大量收购独家精品文档,联系QQ:2885784924

详解Python中代码缩进(Indent).docx

  1. 1、本文档共12页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
详解Python中代码缩进(Indent)

【教程】详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果/tutorial_python_indent/2012 年 12 月 24 日 下午 1:49/author/crifan/crifan/tutorial_python_indent/已有8165人围观/tutorial_python_indent/2个评论先给出几个常见的错误和解决办法:Python中常见的和代码缩进有关的问题IndentationError: unexpected indent举例:这样的代码:# -*- coding: utf-8 -*-import pickleimport math ipath = D:/123 fileobj= open(ipath, rb) pdata= pickle.load(fileobj) fileobj.close()运行结果是:D:\tmp\tmp_dev_root\python\pick_dump_errorpickle_dump.py File D:\tmp\tmp_dev_root\python\pick_dump_error\pickle_dump.py, line 4 ipath = D:/123 ^IndentationError: unexpected indent原因:Python解析器,发现你的代码缩进有问题。此处的问题是,在import math之后,突然来了个: ipath = D:/123而此种缩进,前面即不是函数定义:def someFunction(): xxx xxx也不是其他的形式,所以,语法上,就不支持,即Python解析器,不知道这段代码,是属于哪个范围的,无法解析这样的代码。?代码执行无结果比如,这样的代码:#!/usr/bin/python# -*- coding: utf-8 -*-Function:【教程】详解Python中代码缩进(Indent)/tutorial_python_indentAuthor:Crifan LiVersion: 2012-12-24Contact: admin at crifan dot comdef indentDemo(): print Just demo for python code indent; #if __name__ == __main__: indentDemo();执行出来的结果是空的:D:\tmp\tmp_dev_root\python\python_indentpython_indent.pyD:\tmp\tmp_dev_root\python\python_indent即,啥都没输出,这和我们要的结果,明显不符。其原因在于:上述代码中的: indentDemo();是属于函数indentDemo中的代码,而不是此脚本所能执行出来的代码。而想要达到我们要的效果,即能够执行到对应的函数indentDemo,可以改为:#!/usr/bin/python# -*- coding: utf-8 -*-Function:【教程】详解Python中代码缩进(Indent)/tutorial_python_indentAuthor:Crifan LiVersion: 2012-12-24Contact: admin at crifan dot comdef indentDemo(): print Just demo for python code indent; if __name__ == __main__: indentDemo();或#!/usr/bin/python# -*- coding: utf-8 -*-Function:【教程】详解Python中代码缩进(Indent)/tutorial_python_indentAuthor:Crifan LiVersion: 2012-12-24Contact: admin at crifan dot comdef indentDemo(): print Just demo for python code indent; #if __name__ == __main__:indentDemo();输出的结果都是:D:\tmp\tmp_dev_root\python\python_indentpython_indent.pyJust demo for python code indentD:\tmp\tmp_dev_root\python\python_indentpython_indent.py再来解释具体的含义:Python中的代码缩进Python中,是通过代码的缩进,来决定代码的逻辑的。通俗的说,Python中的

文档评论(0)

xcs88858 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:8130065136000003

1亿VIP精品文档

相关文档