我正在尝试创建登录信息。
我不确定如何创建/导入用户名和密码库;我正在研究寻找答案,但无论如何都要问。
将用户名与密码匹配 //(部分解决;需要添加多个具有匹配密码的用户名)
如果密码不正确,如何创建循环?如果输入的密码不正确,则需要再次提示用户输入密码。 //(已解决;使用[print]而不是[return])
如何将循环限制为特定次数的密码尝试。
以下是我的尝试:
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(): msgstr“”“提示输入用户名和密码,反复提示直到它工作。 仅在成功时返回True。 “”“
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
登录()
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'
登录() 用户名:wronguser 找不到用户 usernamepi 密码:wrongpass 请再试一次 password123 '获得授权'
#first尝试更新前感谢: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'
我需要创建循环以再次提示输入密码。
答案 0 :(得分: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语句,我建议你查看一些教程,比如this one。 请注意,如果您返回某些内容,该功能将从此处退出,因此系统将永远不会提示用户输入密码。在上面的代码中,我将返回更改为print语句。
答案 1 :(得分: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()