2014-01-20 182 views
0

你好,我剛剛寫了一個腳本,有點麻煩。我收到一個自我沒有定義的錯誤。雖然我確定我有它的定義。我錯過了什麼嗎?任何意見將不勝感激。謝謝Raspberry Pi Python腳本未定義?

錯誤

File "door_controllerTEST_V4_RFID.py", line 69, in <module> 
    class RfidFileAuthenticator: 

    File "door_controllerTEST_V4_RFID.py", line 76, in RfidFileAuthenticator 
    print "reading from " + self.filename + " file" 

NameError: name 'self' is not defined 

腳本

class RfidInput: 
    def getInput(self): 
     print "waiting for tag" 
     tag = raw_input() 
     return AuthToken(None,tag) 

class RfidFileAuthenticator: 
    filename = "tags.txt" 
    tags = dict() 
    def __init__(self): 
     self.readFile() 
    def readfile(self): 
     secrets = open(self.filename, 'r') 
    print "reading from " + self.filename + " file" 
    for line in secrets: 
      line = line.rstrip('\n') 
      id, tag = line.split(',') 
      self.tags[tag] = id 
    def check(self,token): 
     print "checking if " + token.secret + " is valid" 
     if token.secret in self.tags: 
      print "tag found belonging to: " + self.tags[token.secret] 
      return True 
     else: "tag not found" 
     print 
     return False 

爲我行的缺口是錯誤的correting我行即時得到一個實例沒有屬性「READFILE」

File "door_controllerTEST_V4_RFID.py", line 99, in <module> 
    main() 

    File "door_controllerTEST_V4_RFID.py", line 92, in main 
    authenticator = RfidFileAuthenticator() 

    File "door_controllerTEST_V4_RFID.py", line 73, in __init__ 
    self.readFile() 

AttributeError: RfidFileAuthenticator instance has no attribute 'readFile' 
後雖然

我的腳本現在看起來如何

class RfidInput: 
    def getInput(self): 
     print "waiting for tag" 
     tag = raw_input() 
     return AuthToken(None,tag) 

class RfidFileAuthenticator: 
    filename = "tags.txt" 
    tags = dict() 
    def __init__(self): 
     self.readFile() 
    def readfile(self): 
     secrets = open(self.filename, 'r') 
     print "reading from " + self.filename + " file" 
     for line in secrets: 
       line = line.rstrip('\n') 
       id, tag = line.split(',') 
       self.tags[tag] = id 
    def check(self,token): 
     print "checking if " + token.secret + " is valid" 
     if token.secret in self.tags: 
      print "tag found belonging to: " + self.tags[token.secret] 
      return True 
     else: "tag not found" 
     print 
     return False 
+0

縮進錯誤? – rodrigo

回答

0

它看起來像縮進是錯誤的。 readfile方法中包括print "reading from " + self.filename + " file"之後的所有行應該向右移動一個縮進級別。

更新。新錯誤的原因是您定義了readfile,但撥打readFile(大寫字母F)。 Python是一種區分大小寫的語言,您必須保持一致。

+0

哈哈這是一個簡單的錯誤,雖然現在我得到一個沒有屬性'readFile'我在上面的描述中發佈了完整的錯誤@Sergey – Celerium

+0

非常感謝,我一直在這個腳本上運行一段時間,我想我應該休息一下有一點我再犯小錯誤之前。非常感謝你的幫助 :) – Celerium