[英]Indentation Error python
我正在使用扭曲的API,並正在通過此示例。 我在帶有正確縮進的“ getdummydata”中插入了一個打印語句打印。 代碼如下:
from twisted.internet import reactor, defer
def getDummyData(x):
"""
This function is a dummy which simulates a delayed result and
returns a Deferred which will fire with that result. Don't try too
hard to understand this.
"""
print "in getdummydata"
d = defer.Deferred()
# simulate a delayed result by asking the reactor to fire the
# Deferred in 2 seconds time with the result x * 3
reactor.callLater(2, d.callback, x * 3)
return d
def printData(d):
"""
Data handling function to be added as a callback: handles the
data by printing the result
"""
print d
d = getDummyData(3)
d.addCallback(printData)
# manually set up the end of the process by asking the reactor to
# stop itself in 4 seconds time
reactor.callLater(4, reactor.stop)
# start up the Twisted reactor (event loop handler) manually
reactor.run()
但是,當我運行代碼時,它會出現以下縮進錯誤:
File "C:\Python26\programs\twisttest.py", line 9
print "in getdummydata"
^
IndentationError: unexpected indent
請誰能解釋為什么?
看起來所有函數的“ def”前面都有一個空格。 在我看來,“ def”位於“從”上方的“ r”之下,而不是“ f”之下。
也許如果您刪除這些空格,問題將消失。 空格對於Python很重要。
檢查您沒有混合使用空格和制表符。
此錯誤只能來自錯誤的縮進。 這意味着before的文檔字符串和print語句具有不同的縮進。 即使它們看起來正確對齊,也可能混合使用制表符和空格。 只需重做縮進或從您的問題中復制代碼-即可自行修復;-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.