Python:匹配用户名和密码;如果错误则提示输入密码;
我正在尝试创建一个登录功能。
我不太确定怎么创建或导入一个包含用户名和密码的库;我现在正在研究这个问题,但还是想问问。
要把用户名和密码匹配起来(这个部分已经解决了一些;需要添加多个用户名和对应的密码)。
如果密码错误,怎么创建一个循环?如果输入了错误的密码,用户需要再次被提示输入密码。
怎么限制密码输入的尝试次数。
下面是我尝试过的:
def check_password(user, password):
""" Return True if the user/pass combo is valid and False otherwise. """
# Code to lookup users and passwords goes here. Since the question
# was only about how to do a while loop, we have hardcoded usernames
# and passwords.
return user == "pi" and password == "123"
def login():
""" Prompt for username and password, repeatedly until it works.
Return True only if successful.
"""
try:
while True:
username = raw_input('username:')
password = raw_input('password:')
if check_password(username, password):
break
else:
print "Please try again"
print "Access granted"
return True
except:
return False
用于测试的:login()
。
这个修复了如果密码错误时没有循环提示的问题,因为我用了return
而不是print
;用了if
而不是while
。
def login():
#create login that knows all available user names and match to password ; if password is incorect returns try again and propmts for password again#
username = raw_input('username:')
if username !='pi':
#here is where I would need to import library of users and only accept those usernames; needs to be like 'pi' or 'bob' or 'tim'etc.
print'user not found'
username = raw_input('username')
password = raw_input('password:')
#how to match password with user? store in library ?
while password != '123':
print 'please try again' # You have to change the 'return' to 'print' here
password = raw_input('password')
return 'access granted'
#basically need to create loop saying 'try again' and prompting for password again; maybe smarter to ask limited number of
#times before returning 'you have reached limit of attempts#
if password == '123':
#again matching of passwords and users is required somehow
return 'access granted'
>>> login()
username:wronguser
user not found
usernamepi
password:wrongpass
please try again
password123
'access granted'
>>>
在更新之前的第一次尝试,感谢Merigrim的帮助:
def login():
# Create login that knows all available user names and match to password;
# if password is incorect returns try again and propmts for password again#
username = raw_input('username:')
if username !='pi':
# Here is where I would need to import library of users and only
# accept those usernames; needs to be like 'pi' or 'bob' or 'tim'etc.
return 'user not found'
password = raw_input('password:')
# How to match password with user? store in library?
if password != '123':
return 'please try again'
password = raw_input('password:')
if password != '123':
return 'please try again'
# Basically need to create loop saying 'try again' and prompting
# for password again; maybe smarter to ask limited number of
# times before returning 'you have reached limit of attempts
elif password == '123':
# Again matching of passwords and users is required somehow
return 'access granted'
现在的工作方式是这样的:
>>> login()
username:pi
password:123
'access granted'
>>> login()
username:pi
password:wrongpass
'please try again'
我需要创建一个循环来再次提示输入密码。
2 个回答
1
这里有另一个解决方案,它把用户名和密码分开处理,并且加了一个异常处理器,以防有人试图中断输入。
另外,顺便提一下,最好把用户名和密码一起处理,这样可以防止黑客知道哪些是有效的用户名,哪些不是。
def check_password(user, password):
""" Return True if the user/pass combo is valid and False otherwise. """
# Code to lookup users and passwords goes here. Since the question
# was only about how to do a while loop, we have hardcoded usernames
# and passwords.
return user == "pi" and password == "123"
def login():
""" Prompt for username and password, repeatedly until it works.
Return True only if successful.
"""
try:
while True:
username = raw_input('username:')
password = raw_input('password:')
if check_password(username, password):
break
else:
print "Please try again"
print "Access granted"
return True
except:
return False
# For testing
login()
2
你想要的其实是 while 语句。
不要像这样把 if 语句嵌套在一起:
if password != '123':
return 'please try again'
password = raw_input('password:')
if password != '123':
return 'please try again'
elif password == '123':
return 'access granted'
你可以这样做:
while password != '123':
print 'please try again' # You have to change the 'return' to 'print' here
password = raw_input('password:')
return 'access granted'
这样会一直让用户输入密码,直到输入正确的密码为止。如果你想更深入了解 while 语句,我建议你看看一些教程,比如 这个。
请注意,如果你在函数里使用 return 语句,函数会在那一行结束,这样用户就不会再被要求输入密码了。在上面的代码中,我把 return 改成了 print 语句。