簡體   English   中英

期望一個縮進的塊python

[英]expected an indented block python

>>> words = ['cat', 'window', 'defenestrate']
>>> for w in words:
... print w, len(w)
  File "<stdin>", line 2
    print w, len(w)
        ^
IndentationError: expected an indented block
>>> print w, len(w)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'w' is not defined
>>> for w in words:
... print w, len(w)
  File "<stdin>", line 2
    print w, len(w)
        ^
IndentationError: expected an indented block

我正在通過主要文檔學習Python。 本章 (4.2。對於陳述)。但是當我在UBUNTU TERMINAL上練習時,它給了我上面的錯誤? 那是什么意思 ?

正如它所說,你的print語句需要縮進,因為它在for循環中

>>> words = ['cat', 'window', 'defenestrate']
>>> for w in words:
...     print w, len(w)
cat 3
window 6
defenestrate 12

在python中,縮進不僅僅是為了可讀性,它們是一個要求。 以下兩個不是一回事

if 1==1:
print 'yes'   #incorrect indentation, not accepted by python

if 1==1:
    print 'yes'   #correct indentation, accepted by python

縮進基本上是在代碼中的某些行之前使用制表符空格放置空格

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM