2011-08-06 213 views
1

我想创建一个登录。Python:匹配用户名w /密码;提示输入密码,如果不正确

我不确定如何创建/导入用户名和密码库;我正在研究寻找答案,但以任何一种方式问。

匹配用户名登录密码 //(部分解决;需要有匹配的密码,添加多个用户名)

如何创建一个循环,如果密码不正确?如果输入的密码不正确,则需要再次提示用户输入密码。 //(解决;使用[打印]而不是[返回])

如何将循环限制到一定数量的密码尝试。

下面是我曾尝试:

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" 

高清登录(): “” “提示用户名和密码,直至它的工作原理 返回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 

出于测试

登录()

由于使用'return'而不​​是'print',这个修正了缺少循环提示的问题。和 '如果',而不是“而

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 '已授权访问'

Merigrim:) DEF登录(:210

更新由于之前#first尝试

# 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

你想要的是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。 请注意,如果你返回一些东西,该功能将退出,所以用户永远不会被提示输入密码。在上面的代码中,我更改了返回到打印语句。

+0

谢谢。我正在阅读你的链接。是的,似乎解决了密码不被提示输入不正确。 – PythagorasPi

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() 
+0

是的是的!对此感激不尽 。从最后一个答案我开始了解'while'而不是'if' – PythagorasPi

相关问题