程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(1)

[Python学习]错误篇一:SyntaxError: invalid syntax & IndentationError: expected an indented block

发布于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黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

0 0
收藏该文
已收藏

评论内容:(最多支持255个字符)