Seej
Seej

Reputation: 13

Why does this code not end after the break statement?

I was wondering why the break statement in this code doesn't end it? After break it starts the whole code over again, no?

u = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']    
while True:
    password = input('Choose a new password. (6 or more of letters and numbers only with at least 1 capitalized): ')
    if len(password) >= 6:
        if password.isalnum():
            for i in password:
                if i in u:
                    print('Thank you, your new password is set up.')
                    break
            else:
                print('letters and numbers only with at least 1 uppercase letter.')
        else:
            print('Only letters or numbers.')
    else:
        print('Thats too short for a password')

Upvotes: 0

Views: 220

Answers (2)

s3bw
s3bw

Reputation: 3049

As per the documentation:

It terminates the nearest enclosing loop

Since the for loop is the closest loop, it will terminate. However you are still within the while loop, and this hasn't been terminated.

I would instead close the conditions into functions, since this allows you to clarify the purpose of each loop:

  1. The for to check each character in the password.
  2. The while to ensure a valid password is set.
def check_length(pw):
    if len(pw) >= 6:
        return True
    return False

def contains_uppercase(pw):
    for c in pw:
        if c in "ABC...XYZ":
            return True
    return False

def valid_password(password):
    if check_length(password):
        print('Thats too short for a password')
        return False

    elif password.isalnum():
        print('letters and numbers only with at least 1 uppercase letter.')
        return False

    elif contains_uppercase(password):
        print('Thank you, your new password is set up.')
        return True

    return False

while True:
    password = input('Choose a new password. (6 or more of letters and numbers only with at least 1 capitalized): ')
    if not valid_password(password):
        print('Please try again.')
    else:
        break

Upvotes: 0

Saddy
Saddy

Reputation: 1581

Like SuperStew said, it is only breaking out of the for loop. Alternatively, you could set the while loop on a variable and edit that instead of the break.

notDone = True
while notDone:
    password = input('Choose a new password. (6 or more of letters and numbers only with at least 1 capitalized): ')
    if len(password) >= 6:
        if password.isalnum():
            for i in password:
                if i in u:
                    print('Thank you, your new password is set up.')
                    notDone = False
            else:
                print('letters and numbers only with at least 1 uppercase letter.')
        else:
            print('Only letters or numbers.')
    else:
        print('Thats too short for a password')

Upvotes: 1

Related Questions