你好,我剛剛寫了一個腳本,有點麻煩。我收到一個自我沒有定義的錯誤。雖然我確定我有它的定義。我錯過了什麼嗎?任何意見將不勝感激。謝謝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
縮進錯誤? – rodrigo