3 回答
![?](http://img1.sycdn.imooc.com/5458477300014deb02200220-100-100.jpg)
TA贡献1846条经验 获得超7个赞
为什么压痕很重要?
{}
IndentationError
TabError
.
一点历史
{}
Python对缩进的使用直接来自于ABC,但是这个想法并不是起源于ABC-它已经由DonaldKnuth提出了并且是一个众所周知的编程风格的概念。(Occam编程语言也使用了它。)然而,abc的作者确实发明了冒号的使用,它将引号子句从缩进块中分离出来。在没有冒号的早期用户测试之后,人们发现,对于初学者来说,缩进的含义并不清楚,他们学习了编程的第一步。冒号的添加清楚地说明了这一点:冒号某种程度上引起了人们对以下内容的关注,并以正确的方式将前后的短语联系在一起。
我如何缩进我的代码?
def perm(l): # Compute the list of all permutations of l if len(l) <= 1: return [l] r = [] for i in range(len(l)): s = l[:i] + l[i+1:] p = perm(s) for x in p: r.append(l[i:i+1] + x) return r
每个缩进级别使用4个空格。
def perm(l): # Compute the list of all permutations of l if len(l) <= 1: return [l] r = [] for i in range(len(l)): s = l[:i] + l[i+1:] p = perm(s) for x in p: r.append(l[i:i+1] + x) return r
我还能用标签吗?
空格是首选的缩进方法。
应该只使用选项卡来保持与已经缩进制表符的代码的一致性。
TabError
-t
-tt
Python 3不允许混合使用制表符和空格进行缩进。
带有制表符和空格混合缩进的Python 2代码应该转换为只使用空格。
当使用-t选项调用Python2命令行解释器时,它会发出关于非法混合制表符和空格的代码的警告。当使用-TT时,这些警告将成为错误。强烈推荐这些选项!
“IndationError:意外缩进”是什么意思?
问题
>>> print('Hello') # this is indented File "<stdin>", line 1 print('Hello') # this is indented ^IndentationError: unexpected indent
can_drive = True
if
>>> age = 10>>> can_drive = None>>> >>> if age >= 18:... print('You can drive')... can_drive = True # incorrectly indented File "<stdin>", line 3 can_drive = True # incorrectly indented ^IndentationError: unexpected indent
固定
print
>>> print('Hello') # simply unindent the lineHello
if
can_drive = True
if
>>> age = 10>>> can_drive = None>>> >>> if age >= 18:... print('You can drive')... can_drive = True # indent this line at the same level....
“IndationError:预期的缩进块”是什么意思?
问题
if <condition>:
while <condition>:
if
>>> if True:... File "<stdin>", line 2 ^IndentationError: expected an indented block
for
for
for
>>> names = ['sarah', 'lucy', 'michael']>>> for name in names:... print(name) File "<stdin>", line 2 print(name) ^IndentationError: expected an indented block
>>> if True:... # TODO... File "<stdin>", line 3 ^IndentationError: expected an indented block
固定
>>> names = ['sarah', 'lucy', 'michael']>>> for name in names:... print(name) # The for loop body is now correctly indented.... sarah lucy michael
pass
pass
pass
:
PASS是一个空操作-当它被执行时,什么都不会发生。当语句在语法上是必需的,但不需要执行代码时,它作为占位符非常有用,例如: def f(arg): pass # a function that does nothing (yet)class C: pass # a class with no methods (yet)
if
pass
>>> if True:... pass # We don't want to define a body.... >>>
“INDISTationError:UNINENDENT不匹配任何外部缩进级别”是什么意思?
问题
print
>>> if True:... if True:... print('yes')... print() File "<stdin>", line 4 print() ^IndentationError: unindent does not match any outer indentation level
固定
if
if
>>> if True:... if True:... print('yes')... print() # indentation level now matches former statement's level.... yes>>>
我仍然得到一个IndationError,但我的程序似乎是正确的缩进。我做什么好?
IndentationError
“TabError:缩进中不一致地使用制表符和空格”是什么意思?
问题
TabError
>>> if True:... if True:... print()... print()... print() File "<stdin>", line 5 print() ^TabError: inconsistent use of tabs and spaces in indentation
特例
TabError
>>> if True:... if True: # tab... pass # tab, then 4 spaces... >>>
IndentationError
TabError
>>> if True:... pass # tab... pass # 4 spaces File "<stdin>", line 3 pass # 4 spaces ^IndentationError: unindent does not match any outer indentation level
-t
-tt
固定
关于“SyntaxError”相关缩进问题的注记
SyntaxError
if True: passpass # oops! this statement should be indented!.else: pass
SyntaxError is raised
:
Traceback (most recent call last): File "python", line 4 else: ^SyntaxError: invalid syntax
SyntaxError
pass
pass
if
else
我仍然很难使用Python的缩进语法。我做什么好?
获取一个编辑器,该编辑器将在出现缩进错误时告诉您。有些商品如上面所说, ,和 当你缩进你的代码时,大声地数一数你按空格键(或标签键)的次数。例如,如果您需要将一行缩进四个空格,您就会大声说出“ 一, 二, 三, 四
“每次同时按空格键的时候,这听起来很傻,但它有助于训练你的大脑思考你的代码缩进有多深。” 如果您有一个编辑器,看看它是否有自动将制表符转换为空格的选项。 查看其他人的代码。浏览 或 并查看Python代码的示例。 写代码就行了。这是最好的方法。编写Python代码越多,效果就越好。
所用资源
![?](http://img1.sycdn.imooc.com/54584f9d0001219b02200220-100-100.jpg)
TA贡献1804条经验 获得超8个赞
崇高的文本3
崇高文本菜单 > 偏好 > 设置-语法特定 :
崇高的设置
{ "tab_size": 4, "translate_tabs_to_spaces": true}
![?](http://img1.sycdn.imooc.com/5458506b0001de5502200220-100-100.jpg)
TA贡献1898条经验 获得超8个赞
if True: if False: print('foo') print('bar')
if True: if False: print('foo') print('bar')
添加回答
举报