我正在尝试创建一个程序,您可以在其中创建,登录和删除帐户.我制作了两个列表,一个是用户名,另一个是密码.该脚本要求输入,如果您说登录,它会说:
if loginchoice=='login':
choice = raw_input("What is your username? ")
choice2 = raw_input("What is your password? ")
if choice in accounts:
option1=1
else:
option1=0
if choice2 in password:
option2=1
else:
option2=0
if option1==1 and option2==1:
print "Welcome to Telemology,", choice
else:
print "The username or password you entered is incorrect. Please try again or register."
如您所见,它仅检查输入是否已在列表中.它不会看输入是否具有相同的索引.我不能把“accounts.index(choice)”,因为它将’choice’视为一个整数.由于字符串,引用也是如此.它不会将它们视为变量.有没有办法解决?
我希望,如果我的问题得到解答,两个人将不会同时注册并使指数出现故障.
解决方法:
你想要的是一个映射,或一个字典.它的关键是用户名,其内容是该用户名的密码.
users = {} # this is the mapping
users["joe"] = "123" # it currently contains username "joe" with password "123"
...
username = raw_input("What is your username? ")
password = raw_input("What is your password? ")
if username in users.keys():
expected_password = users[username]
if expected_password == password:
print "Welcome to Telemology,", username
else:
print "Didn't you forget your password,", username
else:
print "Unknown user"
当然,在真实系统中,您可以存储盐渍和加密的密码.