在Python中链接用户名和密码

我编写了一段练习代码,要求用户从这个模拟数据库中输入用户名和密码。我的问题是如何使任何给定用户的用户名与密码相关联?所以用户“amy”的密码将是“apple”。我是否只需要一个变量设置作为字典,或者类似的东西?


list= ["amy", "chris", "jake"]


password = ["apple", "orange", "date"]


login = ("")


counter = 0


attempts = 5


out_of_attempts = False


while login not in list and not (out_of_attempts):


    if counter < attempts:

        login = input ("enter username: ")

        counter += 1

    else:

        out_of_attempts = True


if out_of_attempts:


        print ("Sorry login limit exceeded please try again later")


else:

        pass

        

while login not in password and not (out_of_attempts):


        if counter < attempts:

            login = input ("now password please: ")

            counter += 1

        else:

            out_of_attempts = True

    

            

if out_of_attempts:


        print ("sorry password limit exceeded, try again later")


else:

    print ("thank you please wait")


炎炎设计
浏览 52回答 3
3回答

慕丝7291255

是的,字典设置会更好:auth&nbsp;=&nbsp;{'amy':&nbsp;'apple'....等等。代码修改不会那么难。获取用户的密码(也可以使用它来设置)auth[login]

偶然的你

您的用户名/密码的等效“映射”可以如下完成:credentials = {&nbsp; &nbsp; 'amy': 'apple',&nbsp; &nbsp; 'chris': 'orange',&nbsp; &nbsp; 'jake': 'date',}这些允许您快速“检查”,例如:(username in credentials返回True或False)查看用户名是否有密码;credentials[username]使用等获取给定用户名的密码。

MMMHUHU

简单、稍微安全的方式,让您存储的不仅仅是密码import hashlibdb = {}hash = lambda x: hashlib.md5(x.encode()).hexdigest()def register(user, password, mail):&nbsp; &nbsp; db[user] = {"password": hash(password), "mail": mail}def login(user, password):&nbsp; &nbsp; if db[user]["password"] == hash(password):&nbsp; &nbsp; &nbsp; &nbsp; print("success!")&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print("fail")register("ironkey", "password123", "example@example.com")login("ironkey", "password")login("ironkey", "password123")# get credentials for the user ironkeyprint(db["ironkey"])failsuccess!{'password': '482c811da5d5b4bc6d497ffa98491e38', 'mail': 'example@example.com'}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python