2010-08-25 82 views
0

我正在使用扭曲的API並正在通過這個例子。 我插入了一個打印語句打印「在getdummydata」正確的縮進。代碼如下:縮進錯誤python

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 

請任何人都可以解釋,爲什麼?

+2

工作對我來說很好,正如預期的那樣。確保你到處都有4個空格,並且沒有標籤。 – delnan 2010-08-25 19:30:04

回答

1

它看起來像你的所有功能的「def」在它們前面有一個空格。在我看來,「def」屬於上面「from」的「r」而不是「f」。

也許如果你刪除這些空間,問題就會消失。空格對Python很重要。

+1

我認爲這只是一個SO格式化錯誤...我已經注意到它經常在 – froadie 2010-08-25 19:30:02

+0

是啊..其實我從網頁複製它,我使用np ++。它沒有做出正確的格式。當我自己編寫代碼時,它工作正常。謝謝 – Shwetanka 2010-08-25 19:40:18

0

檢查您是否混合了空格和製表符。

0

此錯誤只能來自錯誤的縮進。這意味着之前的文檔字符串和print語句具有不同的縮進。即使它們看起來正確對齊,也可能會混合使用製表符和空格。只需重做縮進或從你的問題複製代碼 - 所以它自己修復它;-)