我正在制作一个使用简单字典存储帐户名和密码的小型登录系统。我想知道是否有可能使用电子邮件和用户名,然后使用对两者都有效的密码。例如,我唯一的帐户是密码为“ h1”的“ hello”,我如何才能使密码为“ h1”的“ [email protected]”或“ hello”解锁?
accounts = {'hello':'h1'}
print(accounts)
started = ""
while started != "q":
started = input("Are you a registered user y/n: (if you want to quit press any key)")
if started == "n":
createLogin = input("Create your desired login name: ")
if createLogin in accounts:
print ("Login name already exist!")
else:
createPassw = input("Create your desired password password: ")
accounts[createLogin] = createPassw
print("User created!")
addemail = input("What is your email address :")
accounts[createLogin].append(addemail)
elif started == "y":
login = input("Enter your login name: ")
if login in accounts:
passw = input("Enter your password")
else:
print("user doesnt exist.")
if login in accounts and accounts[login] == passw:
print("Login successful!")
changepassword = input("Would you like to change your password?")
if changepassword in ["Y", "y"]:
newpassw = input("What do you want your new password to be?")
accounts[login] = newpassw
print("password succesfully changed!")
else:
print("Password was not changed.")
else:
print("Incorrect password!")
我试图将电子邮件添加到用户名,以便字典看起来像{['hello @ mail.com,'hello']:'h1'}。有任何想法吗?
你是说这里
accounts[createLogin].append(addemail)
那当然是行不通的,因为值是string,而string没有.append
。同样,将电子邮件附加到密码上也不会很紧张。
我认为这不是一个好的结构: {['[email protected],'hello']:'h1'}
我会accounts
列出一些字典:
accounts = [
{
'email': '[email protected]',
'username': 'hello',
'password': 'h1',
},
]
然后,您可以查看是否有具有给定用户名或电子邮件的帐户(我将使用function,希望可以):
def find_account_by_login(accounts, login):
for account in accounts:
if account['email'] == login or account['username'] == login:
return account
# no account matched the login
return None
elif started == "y":
login = input("Enter your login name: ")
account = find_account_by_login(accounts, login)
if not account:
print("user doesnt exist.")
else:
passw = input("Enter your password")
if passw != account['password']:
print("Incorrect password!")
else:
print("Login successful!")
# change the password...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句