亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在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", "[email protected]")


login("ironkey", "password")

login("ironkey", "password123")


# get credentials for the user ironkey

print(db["ironkey"])

fail

success!

{'password': '482c811da5d5b4bc6d497ffa98491e38', 'mail': '[email protected]'}


查看完整回答
反對 回復 2024-01-04
  • 3 回答
  • 0 關注
  • 196 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號