简体   繁体   中英

How do I deal with unlimited loop problems?

so i have this code

pa=""
newpa=""
use=''
use=input('enter username:')
pa=input('enter password')
while True:
    if len(pa)==12:
        newpa=input('re-enter password:') 
    else:
        print('the password used did not meet our system requirements, please enter a 12 digit password')
    if newpa==pa:
        print('you have successfully created a new account!')

whenever I insert a pa that's incorrect the else print keeps looping. How do I make it so whenever I enter a pa that doesn't meet the if to loop back to newpa if this help this is my algorithm alogrythm of what I am trying to go for

First thing to do would be the put the input statement inside the while loop. At the moment your code loops back to the top of the while loop without giving the user a chance to re-enter their password.

You'll also want to move the check for the re-entry part so that it only executes if the first password was valid. And let the user know if their passwords didn't match.

Finally, you'll want to break the loop when a valid password is entered and verified. Use break for this.

The final code would look like this:

pa=""
newpa=""
use=''
use=input('enter username:')
while True:

    pa=input('enter password')

    if len(pa)==12:
        newpa=input('re-enter password:')
        if newpa==pa:
            print('you have successfully created a new account!')
            break
        else:
            print('the passwords did not match!')
    else:
        print('the password used did not meet our system requirements, please enter a 12 digit password')

Hope that helps and makes sense!

Your loop will run continuously until you add break statement.

You can try something like below,

pa=""
newpa=""
use=''
use=input('enter username:')

while True:

   pa=input('enter password')

   if len(pa)==12:
      newpa=input('re-enter password:')
      if newpa==pa:
       print('you have successfully created a new account!')
       break

   else:
      print('the password used did not meet our system requirements, please enter a 12 digit password')

   

When you enter in the last if you want to exit from loop. So, add break after print.

if newpa==pa: 
    print('you have successfully created a new account!')
    break

Use a while True loop and break from that loop if the password meets the requirements. Then you can enter the password a second time and compare if both inputs match.

username = input('enter username:')
while True:
    password = input('enter password')
    if len(password) == 12  and password.isdigit():
        break
    print('the password used did not meet our system requirements, please enter a 12 digit password')

password_check = input('re-enter password:')
if password == password_check:
    print('you have successfully created a new account!')

You might want to wrap that in an additional loop if the passwords don't match.


If you use functions you can reduce the complexity of the main part.

def enter_password(message):
    while True:
        password = input(message)
        if len(password) == 12 and password.isdigit():
            break
        print('the password used did not meet our system requirements, please enter a 12 digit password')
    return password


def main():
    username = input('enter username:')
    while True:
        password = enter_password('enter password')
        password_match = enter_password('re-enter password')
        if password == password_match:
            break
        print("The passwords don't match.")

    print('you have successfully created a new account!')


if __name__ == '__main__':
    main()

Please use break to break the statement and continue to continue the loop statement according to your requirement. I hope the following code meets your requirement. Thanks and let me know if there's anything I can help with.

user=input('enter username:')

while True:
    pa=input('enter password:')
    if len(pa)==12:
        newpa=input('re-enter password:')
        while newpa!=pa:
            newpa=input('re-enter password:')
    else:
        print('the password used did not meet our system requirements, please enter a 12 digit password')
        continue;
    
    if (newpa==pa):
        print('you have successfully created a new account!')
        break;

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