2015-04-02 82 views
-2

是我還是解釋器?我在代碼中看不到縮進錯誤,但它一直告訴我存在錯誤!我使用自動縮進,所以它應該沒問題。當存在縮進錯誤時,我退格並再次縮進,似乎可以修復它,因爲縮進錯誤不再在該行上,而是在另一行上。有人能告訴我什麼是錯Python縮進當沒有縮進錯誤時出錯

class LogicGate: 
    def __init__(self,n): 
     self.label = n 
     self.output = None 

    def getLabel(self): 
     return self.label 

    def getOutput(self): 
     self.output = self.performGateLogic() 
     return self.output 

自我回答:我的IDE的自動縮進縮進是用標籤,有時當我回來的空間,我用空格再次縮進它。所以問題在於混合空格和製表符。我建議將您的IDE設置爲使用空格縮進或不使用製表符。見https://www.python.org/dev/peps/

+0

提供完整的回溯。由於製表符和空格的使用不一致,這是否是縮進錯誤? – 2015-04-02 15:07:52

+0

爲什麼這是upvoted? – 2015-04-02 15:08:42

+3

看看你的帖子的原始來源,你的'def __init__'和'self.label = n'行使用標籤,其他的東西都使用空格。你應該只使用一個或另一個(最好是空格) – Kevin 2015-04-02 15:10:03

回答

1

您的分頁和空間一定是混在一起。將您的編輯器設置爲4個空格作爲製表符。您也可以打開編輯器上的空白指示器,這可以幫助解決縮進錯誤。

3

這將是,

class LogicGate: 
    def __init__(self,n): 
     self.label = n 
     self.output = None 

    def getLabel(self): 
     return self.label 

    def getOutput(self): 
     self.output = self.performGateLogic() 
     return self.output 
+0

OP的可運行示例:http://repl.it/gma/1 – 2015-04-02 15:14:30

1

你的類應該是這樣的:

class LogicGate: 

    def __init__(self, n): 
     self.label = n 
     self.output = None 

    def getLabel(self): 
     return self.label 

    def getOutput(self): 
     self.output = self.performGateLogic() 
     return self.output