创建登录系统

我正在尝试创建一个登录系统。我可以在不实现类和函数的情况下制作系统。我想将每个步骤都变成特定的方法,而不是全部写入一个函数。


我的问题是如果字符长度> 5或密码错误,如何恢复登录询问用户名和密码。


如果用户名和密码不在列表中,我该如何将其恢复或需要再次编码?


    class LoginSystem:


        def __init__(self):

            self.user_id = input("Please enter your user id: ")

            self.user_password = input("Please enter your password: ")


        def login(self):

            username = self.user_id

            password = self.user_password

            if len(username) <= 5 and len(password) <= 5:

                print("Logging In")

            else:

                print("Error! Max Length is 5 chars.") #return back to 

                login system


        def check_system(self):

            registered_user = {

             "test@gmail.com": "test"

            }

            if self.user_id in registered_user:

                print("Successful")

            else:

                new_user = input("Id not found! Are you are new user?\n [Y]es or [N]o\n")

                new_user = new_user.lower()

                if new_user == "Y":

                   return back to login system

                elif new_user == "N": #how to return back to main login system

                   new_username = input("Please enter your user id: ")

                   new_userpassword = input("Please enter your password: ")

                else:

                   return #back to login system


慕侠2389804
浏览 112回答 1
1回答

幕布斯7119047

您LoginSystem将错误的数据视为其实例属性。注册用户的集合在方法调用中是不变的;该login方法本身应该提示输入用户 ID 和密码。class LoginSystem:&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; self.users = {"test@gmail.com": "test"}&nbsp; &nbsp; def login(self):&nbsp; &nbsp; &nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; username = input("Please enter your user id: ")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; password = input("Please enter your password: ")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(username) <= 5 and len(password) <= 5 and self.check_system(username, password):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Logging In")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # TODO Disallow infinite retries to get it right&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Error! Max Length is 5 chars.")&nbsp; &nbsp; def check_system(self, name, password):&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; expected_password = self.registered_user[name]&nbsp; &nbsp; &nbsp; &nbsp; except KeyError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Or try to add a new user to the system here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return False&nbsp; &nbsp; &nbsp; &nbsp; if password != expected_password:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return False&nbsp; &nbsp; &nbsp; &nbsp; return True可以添加一个单独的方法来在必要时将新用户添加到系统中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python