0

我正在为我的班级做一个项目,我被困在这个额外的学分部分。我正在构建一个密码和用户名检查器来检查具有用户名和密码的字典列表。

我尝试了很多方法,但似乎无法得到我需要的结果。我拥有的代码的问题是它没有将密码与关联的用户名进行比较。如果密码在任何字典中,则结果为 TRUE。任何帮助将不胜感激,因为我将继续自己努力

This is what I have:

adminList = [
    {
        "username": "DaBigBoss",
        "password": "DaBest"
    },
    {
        "username": "root",
        "password": "toor"
    }
]

#Import Required Tools

import getpass


####################################
###     Define ALL FUNCTIONS     ###
####################################


#Define function to confirm login credntials of the user
def getCreds():
    user_name = input("Please input your user name: ")
    while not any(d["username"] == user_name for d in adminList):
        print("User name not recognized, please try again.")
        getCreds()

    pass_word = getpass.getpass("Please enter your password: ")
    print("Checking Credentials")


    while not any(d["password"] == pass_word for d in adminList):
        print("The password in not correct")
        pass_word = getpass.getpass("Please enter your password again: ")
    else:
        print ("Hello and welcome to the Matrix.")    

#Call the getCreds function        
getCreds()
4

1 回答 1

0

我将避免使用直接的编码示例 - 但要获取用户名和密码,然后将这两个值与您的管理员凭据进行比较。对于该任务,您可能应该将您的 adminList 从字典列表修改为字典字典:

adminList = {"DaBigBoss": {"password": "DaBest}, "root": {"password": "toor"}}

这种安排的好处是双重的:1.更容易查询(查询用户是否匹配,而不是比较密码) 2.保证只能有一个DaBigBoss,更容易防止出现N次相同的用户名。(如果 adminList 中的“DaBigBoss”很简单就足够了)

于 2018-10-11T19:21:54.270 回答