发布于2019-08-08 09:50 阅读(442) 评论(0) 点赞(0) 收藏(4)
REFERENCE:《Head First Python》
ID:我的第一篇[Python学习]
BIRTHDAY:2019.7.6
EXPERIENCE_SHARING:两个程序错误类型
1、错误类型:
1 2 3 4 5 6 7 8 9 | >>> for each_item in movies: if isinstance (each_items, list ): for nested_item in each_item: print (nested_item) else : print (each_item) SyntaxError: invalid syntax >>> |
SyntaxError: invalid syntax
意思就是“语法错误:不正确的语法”
一般是格式上漏掉或者多了些东西。或者字符格式不对。
(1)错误原因:
第二行的 each_items多了一个s
修改后:
1 2 3 4 5 6 7 8 | >>> for each_item in movies: if isinstance (each_item, list ): for nested_item in each_item: print (nested_item) else : print (each_item) SyntaxError: invalid syntax |
(2)错误原因
第二行的冒号似乎和第三行的不一样,可能是中文状态下的冒号。修改试试:
1 2 3 4 5 6 7 8 9 10 11 12 | >>> for each_item in movies: if isinstance (each_item, list ): for nested_item in each_item: print (nested_item) else : print (each_item) File "<pyshell#18>" , line 3 for nested_item in each_item: ^ IndentationError: expected an indented block >>> |
出现新的错误类型:
1 | IndentationError: expected an indented block<br><br>参考下方第二个错误类型的解决方法: |
2、错误类型:
1 2 3 4 5 6 7 8 9 10 11 | >>> for each_item in movies: if isinstance (each_item, list ): for nested_item in each_item: print (nested_item) else : print (each_item) File "<pyshell#13>" , line 3 for nested_item in each_item: print (nested_item) ^ IndentationError: expected an indented block >>> |
1 | <span style = "font-family: 'Microsoft YaHei';" ><strong><span style = "color: #ff0000;" ><br><span style = "font-size: 14px;" >IndentationError: expected an indented block< / span>< / span>< / strong>< / span> |
说明此处需要缩进,你只要在出现错误的那一行,按空格或Tab(但不能混用)键缩进就行。
一句话 有冒号的下一行往往要缩进。
Python语言是一款对缩进非常敏感的语言,给很多初学者带来不少困惑,即便是很有经验的python程序员,也可能陷入陷阱当中。
最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分别的。
(参考 知乎·回答)
第三行缩进后:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | >>> for each_item in movies: if isinstance (each_item, list ): for nested_item in each_item: print (nested_item) else : print (each_item) A B C 1 2 3 HAPPY [ 'sadness' , 'sorrow' , 'moved' ] >>> |
作者:我就是喜欢你
链接:https://www.pythonheidong.com/blog/article/12785/132e5c40b5031001f0b5/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!