python问题:IndentationError:expected an indented block错误解决
来源:互联网 发布:mpi编程 编辑:程序博客网 时间:2024/06/05 06:05
Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的Python程序员,也可能陷入陷阱当中。最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分别的。
Python uses indentation to determine when one line of code is connected to the line above it.
Python Crash Course (英文版)的page 57详细地描述了不正确的缩进所产生的错误,但是python对整个空行是不敏感的。
往往有的人会疑问:我根本就没缩进怎么还是错,不对,该缩进的地方就要缩进,不缩进反而会出错,,比如:
if xxxxxx:
(空格)xxxxx
或者
def xxxxxx:
(空格)xxxxx
还有
for xxxxxx:
(空格)xxxxx
一句话 有冒号的下一行往往要缩进,该缩进就缩进
此外python 的for 循环语句是根据缩进的情况判断for 所覆盖的范围,比如:
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n")
的输出是:
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
David, that was a great trick!
I can't wait to see your next trick, David.
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
然而下面的语句,仅仅是第二个print的缩进不同
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n")
其输出是
Alice, that was a great trick!
David, that was a great trick!
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
.
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- python问题:IndentationError:expected an indented block错误解决
- LeetCode 437 Path Sum III (DFS)
- 使用API网关构建移动端友好的API服务
- 第八周—等差数列
- C++ 对象的内存布局(上)
- 无向图双连通分量(poj-3352)
- python问题:IndentationError:expected an indented block错误解决
- CodeForces 724B Batch Sort
- MySQL之——corosync+pacemaker+drbd+mysql实现MySQL高可用
- Mysql 解决 phpMyAdmin 上传文件 最大限制
- Google's BBR拥塞控制算法如何对抗丢包
- python 数学函数
- hdu4027
- Oracle sql完整的执行顺序:
- MySQL数据库数据类型(char(n),varchar(n),nchar(n),nvarchar(n)的区别)