【问题标题】:Python: matching user names w/ password ; prompted for password if incorrect ;Python:将用户名与/密码匹配;提示输入密码不正确;
【发布时间】:2011-10-21 07:36:53
【问题描述】:

我正在尝试创建登录。

我不确定如何创建/导入用户名和密码库;我正在研究以找到答案,但无论哪种方式都问过。

匹配用户名和密码 //(部分解决;需要添加多个用户名和密码匹配)

如果密码不正确,如何创建循环?如果输入的密码不正确,需要再次提示用户输入密码。 //(已解决;使用 [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"

定义登录(): """ 提示输入用户名和密码,重复直到成功。 仅在成功时返回 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',这个固定的缺少循环提示;和 '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'

登录() 用户名:错误用户 未找到用户 用户名pi 密码:错误密码 请再试一次 密码123 '访问权限'

#更新前的第一次尝试感谢:Merigrim: 定义登录():

    # 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'

我需要创建循环以再次提示输入密码。

【问题讨论】:

    标签: python import passwords matching


    【解决方案1】:

    您想要的是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。 请注意,如果您返回某些内容,该函数将在那里退出,因此永远不会提示用户输入密码。在上面的代码中,我将返回改为打印语句。

    【讨论】:

    • 谢谢。我现在正在阅读您的链接。是的,这似乎解决了输入错误时不提示密码的问题。
    【解决方案2】:

    这是另一个解决方案,其中包含用户名和密码,以及一个异常处理程序,以防有人试图中止输入。

    另外,仅供参考,最好将用户名和密码放在一起,以免让破解者知道什么是有效用户名,什么不是有效用户名。

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

    【讨论】:

    • 是的是的!对此感激不尽 。从最后一个答案开始,我刚刚开始了解“while”而不是“if”
    猜你喜欢
    • 2021-12-11
    • 2016-01-30
    • 2021-04-04
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 2015-01-17
    相关资源
    最近更新 更多