Py学习  »  Python

从Python中的类对象列表中确认用户和密码

José Orlando • 5 年前 • 1504 次点击  

我需要能够验证用户和输入的密码,但是当我运行下面的代码时,我只能验证列表中的第一个元素和第二个元素等没有被验证。

注意:用户和密码作为类对象存储在列表中[如下所示:
admin(user, password)]...

def login(self):
    user_name = input("Please Enter Your Username : ").upper()
    password = input("Please Enter Your Password : ").upper()
    for obj in self.admins:
        while obj.admin_name != user_name and obj.admin_password != password:
            print(" Sorry Username and Password Incorrect Please Re-enter for Validation ")
            user_name = input("Please Enter Your Username : ").upper()
            password = input("Please Enter Your Password : ").upper()
        else:
            print("Greetings,", user_name, "You are Now Logged in the System")
            break
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50371
文章 [ 3 ]  |  最新文章 5 年前
maxxy
Reply   •   1 楼
maxxy    5 年前

简单地删除 break 从你的 else 陈述。

Evgeny Bovykin
Reply   •   2 楼
Evgeny Bovykin    5 年前

当你跑的时候 break 在你的 else 你实际上是在 for 循环。移除 打破 它应该像你期望的那样工作

Anonymous
Reply   •   3 楼
Anonymous    5 年前

你的 while 循环只检查第一个用户名。您应该切换循环的顺序:

def login(self):
    user_name = input("Please Enter Your Username : ").upper()
    password = input("Please Enter Your Password : ").upper()
    while True:
        for obj in self.admins:
            if obj.admin_name == user_name and obj.admin_password == password:
                break
        else:
            print(" Sorry Username and Password Incorrect Please Re-enter for Validation ")
            user_name = input("Please Enter Your Username : ").upper()
            password = input("Please Enter Your Password : ").upper()
            continue
        break
    print("Greetings,", user_name, "You are Now Logged in the System")

这也是一种非常糟糕的检查密码的方法。