简体   繁体   中英

Password Checker loop not working

I have tried to create an app in Python that makes a user create a password and then makes them verify it. Obviously I had to create a loop so that when the user entered an unmatching password they had to try again. I am not very experienced with loops so here is what I got. How do I make this work?

here is the code:

password = raw_input ("Create a password: ")
passwordv = raw_input ("Retype your password: ")
a = ("Passwords don't match!  Please try again!: ")
b = ("Congrats, you have created a password")

def password():
    if password == passwordv :
        print ("Congrats, you have created a password")
    else :
        print (a)
    return password
    return passwordv
while password !=passwordv:
    print (a)

here is another set of code trying to do the same thing:

password = raw_input('Create a password: ')
passwordv = raw_input('Veryify your password: ')

while (password != passwordv):
    print raw_input('Passwords do not match, try again: ')
    if (password == passwordv) :
        print ('Password set')
        break

Your conditional to test whether the passwords match was included in the loop which checks that they don't match -- so, it never gets run.

Try this instead:

password = raw_input('Create a password: ')
passwordv = raw_input('Verify your password: ')

while (password != passwordv):
    print raw_input('Passwords do not match, try again. ')
    password = raw_input('Create a password: ')
    passwordv = raw_input('Verify your password: ')
    continue

print ('Password set')

Note how only the code that should be run if the passwords don't match is included within the while loop.

What you need here is an "N and a half times loop". To avoid repeating code (guided by the DRY principle) you might consider writing a function to get the passwords. The following might work:

def get_passwords():
    password = raw_input('Create a password: ')
    passwordv = raw_input('Veryify your password: ')
    return password, passwordv

pw1, pw2 = get_passwords()
while pw1 != pw2:
    print "Sorry, passwords do not match"
    pw1, pw2 = get_passwords()

The loop won't be entered at all if the original passwords match. Otherwise it will repeat until they do.

password=raw_input('Enter password: \t\t\t')
passwordv=raw_input('Enter password again to verify:  \t')

if password == passwordv:
    print "\ncongratz you have created password"
else:
    print "\nPlease try again...!"

while password != passwordv:
    password=raw_input("Enter password: \t\t\t")
    passwordv=raw_input("Enter password again to verify:  \t")

    if password == passwordv:
        print"\ncongratz you have created password"
    else:
        print"\nPlease try again...!"

I know this one is long, It is just for basic understanding

The other answers provide the code that will do what you want . I thought that it could be interesting to tell you a little bit more what was wrong with the code you provided because the problem is more about your understanding of how things work.

1st attempt: a variable and a function have the same name

# Here you define your password variable as a string
password = raw_input ("Create a password: ")
[...]

# And then you reassign it here: it contains now the address of that function! 
def password():
    [...]


# Now you're comparing a function to a string: they'll never be equal
while password !=passwordv:
    [...]

This could have been avoided simply by changing the name of your function (which is never called by the way so I'm not sure what you wanted to do but I thought you might find that interesting). Further reading: Python: function and variable with same name

2nd attempt: the values of the variables stay the same

password = raw_input('Create a password: ')
passwordv = raw_input('Veryify your password: ')

# At this point, if the test is true, you'll reach the end of the code without
# displaying the 'Password set' message you wanted
while (password != passwordv):
    # This only prints a message
    print raw_input('Passwords do not match, try again: ')
    # The condition below can never be true: you just tested that they were
    # different but did nothing to give them new values!
    if (password == passwordv) :
        print ('Password set')
        break

The way to fix that is what the other answers provide: take your message out of the loop (getting out of the loop means that your condition was met) ; make sure that you do something inside the loop that will assure that the next test will be different from the previous one (otherwise, you will end up testing the same values over and over again) ; to avoid breaking inside the loop, set your values outside the loop, and then get new values inside the loop in case the initial test failed.

Here's an idea: First define these:

MinPass = 6
MaxPass = 12

Then this:

print ("Now, lets try with a password. Please enter one here")
EnteredPassword = input("Password: ")
while len(EnteredPassword) < MinPass:
    print ("That password is too small. Please try again")
    EnteredPassword = input("Password: ")
while len(EnteredPassword) > MaxPass:
    print ("That password is too long, please try again")
    EnteredPassword = input("Password: ")

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM