1 回答
TA贡献1794条经验 获得超8个赞
您LoginSystem将错误的数据视为其实例属性。注册用户的集合在方法调用中是不变的;该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
可以添加一个单独的方法来在必要时将新用户添加到系统中。
添加回答
举报