(class,def,self)AttributeError:“xx”对象没有属性“xx”

2024-09-27 09:34:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我变成了这个错误:

回溯(最近一次呼叫):

文件“xx”,第51行,in

Kontrolle.CheckSign()

文件“xx”,第46行,勾号

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()

Tags: selfinputdefloginencodehashlibprinthexdigest
1条回答
网友
1楼 · 发布于 2024-09-27 09:34:38

移动线路

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

SignUp方法进入类变量,或者为类创建一个__init__方法并在其中初始化它

当你打电话时:

Kontrolle = Sicherheit()

设置变量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():
        ....

    ....

相关问题 更多 >

    热门问题