使用字典的密码模拟器

2024-09-29 23:27:36 发布

您现在位置:Python中文网/ 问答频道 /正文

对于我的类,我应该编写一个代码,询问用户需要输入多少对用户名和密码。之后,应该有那么多的用户名和密码,每行一个。我想我们也应该把它们存到字典里。然后,您要求用户输入用户名和密码,然后与字典中的内容相比,检查组合是否正确。这是我的东西

num_passwords_user = int(input("How many pairs of usernames/passwords? "))
username_input = ''
password_input = ''
counter = 0

while counter < num_passwords_user:
    username_input = str(input("Enter username for the database: "))
    password_input = str(input("Please enter your password for the database: "))
    database = {username_input: password_input}
    counter += 1
    print(database)
    ask1 = input("Enter username to login: ")
    ask2 = input("Enter password to login: ")
    while (ask1 and ask2) not in database:
        if (ask1 and ask2) not in database:
            print("Incorrect, try again.")
            username_input = (input("Enter username: "))
            ask = input("Enter password: ")
    print("Access Granted")

问题在于第二个while循环。当我输入一个值时,它总是打印“不正确,再试一次”。我不知道如何解决这个问题


Tags: 用户密码input字典counterusernamepassworddatabase
2条回答

这就是你需要的

def check(database, user, passw):
    if not(user in database):
        return True
    return not (database[user] == passw)

database = {}
while counter < num_passwords_user:
    username_input = str(input("Enter username for the database: "))
    password_input = str(input("Please enter your password for the database: "))
    database[username_input]=password_input
    counter += 1
    ask1 = input("Enter username to login: ")
    ask2 = input("Enter password to login: ")
    while check(database,ask1,ask2):
        print("Incorrect, try again.")
        ask1 = (input("Enter username: "))
        ask2 = input("Enter password: ")
    print("Access Granted")

第二个循环也没有正确地纠正ask1ask2

数据库只是不匹配,所以请尝试以另一种方式保存数据

while counter < num_passwords_user:
username_input = str(input("Enter username for the database: "))
password_input = str(input("Please enter your password for the database: "))
database =username_input ,password_input

相关问题 更多 >

    热门问题