为了账号安全,请及时绑定邮箱和手机立即绑定

在Python中链接用户名和密码

在Python中链接用户名和密码

炎炎设计 2024-01-04 10:08:52
我编写了一段练习代码,要求用户从这个模拟数据库中输入用户名和密码。我的问题是如何使任何给定用户的用户名与密码相关联?所以用户“amy”的密码将是“apple”。我是否只需要一个变量设置作为字典,或者类似的东西?list= ["amy", "chris", "jake"]password = ["apple", "orange", "date"]login = ("")counter = 0attempts = 5out_of_attempts = Falsewhile login not in list and not (out_of_attempts):    if counter < attempts:        login = input ("enter username: ")        counter += 1    else:        out_of_attempts = Trueif 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")
查看完整描述

3 回答

?
慕丝7291255

TA贡献1859条经验 获得超6个赞

是的,字典设置会更好:

auth = {'amy': 'apple'....

等等。代码修改不会那么难。获取用户的密码(也可以使用它来设置)

auth[login]


查看完整回答
反对 回复 2024-01-04
?
偶然的你

TA贡献1841条经验 获得超3个赞

您的用户名/密码的等效“映射”可以如下完成:


credentials = {

    'amy': 'apple',

    'chris': 'orange',

    'jake': 'date',

}

这些允许您快速“检查”,例如:(username in credentials返回True或False)查看用户名是否有密码;credentials[username]使用等获取给定用户名的密码。


查看完整回答
反对 回复 2024-01-04
?
MMMHUHU

TA贡献1834条经验 获得超8个赞

简单、稍微安全的方式,让您存储的不仅仅是密码


import hashlib


db = {}


hash = lambda x: hashlib.md5(x.encode()).hexdigest()


def register(user, password, mail):

    db[user] = {"password": hash(password), "mail": mail}


def login(user, password):

    if db[user]["password"] == hash(password):

        print("success!")

    else:

        print("fail")


register("ironkey", "password123", "example@example.com")


login("ironkey", "password")

login("ironkey", "password123")


# get credentials for the user ironkey

print(db["ironkey"])

fail

success!

{'password': '482c811da5d5b4bc6d497ffa98491e38', 'mail': 'example@example.com'}


查看完整回答
反对 回复 2024-01-04
  • 3 回答
  • 0 关注
  • 45 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信