PythagorasPi
PythagorasPi

Reputation: 77

Python: matching user names w/ password ; prompted for password if incorrect ;

I am trying to create a login.

I am not sure how to create/import a library of usernames and passwords; I am researching to find an answer at the moment but asked either way.

How to limit loop to certain number of attempts for password.

Below is what I have tried:

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

This fixed lack of loop prompting if wrong password due to using return instead of print; and if instead of 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'
>>> 

First attempt before updating thanks to 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'

Here is how it currently works:

>>> login()
username:pi
password:123
'access granted'
>>> login()
username:pi
password:wrongpass
'please try again'

I need to create loop to prompt again for password.

Upvotes: 1

Views: 3792

Answers (2)

Ray Toal
Ray Toal

Reputation: 88428

Here's another solution with the user name and password factored out, and an exception handler in case someone tries to abort the input.

Also, FYI it is best to take the user and password together so as not to let crackers know what is and is not a valid username.

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

Upvotes: 1

Merigrim
Merigrim

Reputation: 856

What you want is the while statement.

Instead of nesting if-statements like this:

if password != '123':
    return 'please try again'
    password = raw_input('password:')
    if password != '123':
        return 'please try again'
elif password == '123':
    return 'access granted'

You can do this:

while password != '123':
    print 'please try again' # You have to change the 'return' to 'print' here
    password = raw_input('password:')
return 'access granted'

This will continue prompting the user for a password until the right password is entered. If you want to become more familiar with the while statement, I suggest checking out some tutorials, like this one. Please note that if you return something the function will exit there, so the user will never be prompted for a password. In the code above I changed the return to a print statement instead.

Upvotes: 2

Related Questions