为什么我不断收到此错误消息?

时间:2020-10-29 17:09:20

标签: python oop

嗨,我收到此错误:当我要检查我保存的密码时,未定义名称“ passwords”。为什么会出现错误?

def login_or_register(self):

    user_input = input("Login or Register: ")

    if user_input == "login":
        self.login2()
    elif user_input == "register":
        pass
    else:
        print("Try again!")
        self.login_or_register()


def add_password(self):

    global sitepassword
    global site
    Site = input("Enter site name: ")
    sitepassword = input("Enter password")
    self.add_password_orCheck_password()

    global passwords
    self.passwords = {Site: sitepassword}

def check_passwords(self):
    which_password = input("Which sites password do you want to see?")
    for Site in passwords:
        if which_password in passwords:
            print(sitepassword)
            self.add_password_orCheck_password()




def add_password_orCheck_password(self):
    add_new = input("Do you want to add a password or check passwords?")
    if "add a password" in add_new:
        self.add_password()
    elif "check passwords" in add_new:
        self.check_passwords()



Matt = User("Matterson", "matt@gmail.com", "21.12.1999" ,"male", "Password987")
session1 = Matt.login_or_register()

2 个答案:

答案 0 :(得分:0)

check_passwords中,passwords变量仍然是本地变量。是的,您在另一个变量中声明了全局变量,但是还需要在每个使用全局变量的函数中声明它。

答案 1 :(得分:0)

check_passwords内部,变量仍然是局部变量,因此在引用变量之前,您需要在每种方法中使用global passwords