简体   繁体   中英

Dual password authentication in Python

Been trying out some python as I learnt a bit in school and I've been trying to make a simple login system with password authentication.

The way I want it to work: get username and password, ask for password for authentication and if password is not correct then ask again until it gets the right one

this is my code so far:

#online auth

email = input('What is your email address?')
password = input('Enter a password')
passcheck = input('Please re-enter password for confimation')

while passcheck != password:
    input('Please re-enter password for confirmation')

else: print("A confirmation code has been emailed to you")

When I run the program it asks for the email and username properly and then I get the confirmation question. If i enter the same password as the one i inputted in the first place it proceeds to the else statement. If i enter the wrong one it ends a never ending loop of reenter the password, even if i input the correct one.

I know that the while loop created an infinite loop but I cant find any good way to end it.

You could do something like

email = input('What is your email address?')
password_input = False
while not password_input or pass_check != password:
    password = input('Enter a password')
    pass_check = input('Please re-enter password for confimation')
    password_input = True
print("A confirmation code has been emailed to you")

Just solved with the help of my friend:

#online auth

email = input('What is your email address?')
password = input('Enter a password')
passcheck = input('Please re-enter password for confimation')

while passcheck != password:
    passcheck = input('Please re-enter password for confirmation')
else: print("A confirmation code has been emailed to you")

a good way to end a loop is to either use a for, or a counter variable

for index in range(0,5):
    password = input('Enter a password')
    pass_check = input('Please re-enter password for confimation')
    if pass_check == password :
        print("A confirmation code has been emailed to you")
        index=65532
    else :
        print("The passwords do not match, try again.")

if index != 65532 :
    print("too many attempts, if you want to retry, please restart the program")

note : the 65532 is not special, it's just out of range, so you exit the for.

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