2017-10-28 60 views
0

我成爲了這個錯誤:(類,閃避,個體經營)AttributeError的: 'XX' 對象有沒有屬性 'XX'

回溯(最近通話最後一個):

文件 「XX」,行51在

Kontrolle.CheckSign()

文件 「XX」,46行,在CheckSign

if self.isSigned == True:

AttributeError的: 'Sicherheit' 對象沒有屬性 'isSigned'

你能幫我嗎?

import hashlib 
class Sicherheit: 
    passwordFile = 'usercreds.tmp' 
    def Signup(self): 
     self.isSigned = False # !!! self.isSigned 
     print("Sie müssen sich erst anmelden!\n") 
     usernameInput = input("Bitte geben Sie Ihren Nutzername ein: \n") 
     passwordInput = input("Bitte geben Sie Ihr Passwort ein: \n") 
     usernameInputHashed = hashlib.sha512(usernameInput.encode()) 
     passwordInputHashed = hashlib.sha512(passwordInput.encode()) 

     with open(self.passwordFile, 'w') as f: 
      f.write(str(usernameInputHashed.hexdigest())) 
      f.write('\n') 
      f.write(str(passwordInputHashed.hexdigest())) 
      f.close() 

     self.isSigned = True # !!! self.isSigned 
     print("Anmeldung war erfolgreich!\n") 
     print("======================================================\n") 
     self.Login() # Moves onto the login def 

    def Login(self): 
     print("Sie müssen sich einloggen!\n") 

     usernameEntry = input("Bitte geben Sie Ihren Nutzername ein: \n") 
     passwordEntry = input("Bitte geben Sie Ihr Passwort ein: \n") 
     usernameEntry = hashlib.sha512(usernameEntry.encode()) 
     passwordEntry = hashlib.sha512(passwordEntry.encode()) 
     usernameEntryHashed = usernameEntry.hexdigest() 
     passwordEntryHashed = passwordEntry.hexdigest() 

     with open(self.passwordFile) as r: 
      info = r.readlines() 
      usernameInFile = info[0].rstrip() 
      passwordInFile = info[1].rstrip() 

     if usernameEntryHashed == usernameInFile and passwordEntryHashed == passwordInFile: 
      print("Anmeldung war erfolgreich!\n") 

     else: 
      print("Anmeldung war nicht erfolgreich!!!\n") 
      self.Login() 

    def CheckSign(self): 
     if self.isSigned == True: # !!! self.isSigned 
      self.Login() 
     else: 
      self.Signup() 
Kontrolle = Sicherheit() 
Kontrolle.CheckSign() 

回答

1

招行

self.isSigned = False # !!! self.isSigned 

出你SignUp方法和你的類變量,否則創建類的__init__方法,並在那裏

初始化它,當你撥打:

Kontrolle = Sicherheit() 

設置你的vari能夠self.isSigned從未執行(該SignUp方法,它的一部分,這是不執行),所以當你撥打:

Kontrolle.CheckSign() 

它將查找尚未設置變量,然後引發錯誤:

AttributeError: 'Sicherheit' object has no attribute 'isSigned' 

這裏是你如何把它聲明的類中:

class Sicherheit: 
    passwordFile = 'usercreds.tmp' 

    def __init__(self): 
     self.isSigned = False 

    def SignUp(): 
     .... 

    .... 
+0

我怎麼能初始化它在那裏? –

相關問題