[英]Creating a Login System
I am trying to create a login system.我正在尝试创建一个登录系统。 I could make the system without implementing class and just functions.
我可以在不实现 class 的情况下制作系统,而只是功能。 I would like to make each steps into specific methods without writing all into one function.
我想把每一步都做成具体的方法,而不是全部写成一个function。
My question is how to revert back into login asking username and password if the character length is > 5 or wrong password.我的问题是如果字符长度> 5或密码错误,如何恢复登录询问用户名和密码。
If the username and password not in the list how do i revert it back or do i need to code again?如果用户名和密码不在列表中,我该如何将其恢复或需要再次编码?
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
Your LoginSystem
treats the wrong data as its instance attributes.您的
LoginSystem
将错误的数据视为其实例属性。 The set of registered users is constant across method calls;注册用户的集合在方法调用中是不变的; the
login
method itself should be prompting for the user id and password. login
方法本身应该提示输入用户 ID 和密码。
class LoginSystem:
def __init__(self):
self.users = {"test@gmail.com": "test"}
def login(self):
while True:
username = input("Please enter your user id: ")
password = input("Please enter your password: ")
if len(username) <= 5 and len(password) <= 5 and self.check_system(username, password):
print("Logging In")
break
else:
# TODO Disallow infinite retries to get it right
print("Error! Max Length is 5 chars.")
def check_system(self, name, password):
try:
expected_password = self.registered_user[name]
except KeyError:
# Or try to add a new user to the system here
return False
if password != expected_password:
return False
return True
A separate method can be added to add a new user to the system where necessary.可以添加一个单独的方法来在必要时将新用户添加到系统中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.