-1

How you would change the code below so that a new password is asked until the password is strong? I know that you have to use a while loop but I keep making a mess of it every time I try.

digits = False
upcase = False
lowcase = False
alnum = False
password = input("Enter a password between 6 and 12 characters: ")
while len(password) < 6 or len(password)>12:
    if len(password)<6:
        print("your password is too short")
    elif len(password) > 12:
        print("your password is too long")
    password = input("Please enter a password between 6 and 12 characters.")

for i in range(0,len(password)):
    if password[i].isupper():
        upcase = True
    if password[i].islower():
        lowcase = True
    if password[i].isalnum():
        alnum = True
    if password[i].isdigit():
        digits = True

if digits and alnum and lowcase and upcase and digits:
   print("Your password is strong")
elif (digits and alnum) or (digits and lowcase) or (digits and upcase) or (alnum and lowcase) or (alnum and upcase) or (lowcase and upcase):
    print("Your password is medium strength")
else:
    print("Your password is weak")
Nootnoot5
  • 3
  • 1
  • 1
    I think you could easily adapt one of the techniques shown in the answers to the question [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) to do this. In fact, I'm tempted to mark your question as a duplicate… – martineau Nov 22 '20 at 18:05

2 Answers2

1

Try this:

notStrong = True
while notStrong:
    digits = False
    upcase = False
    lowcase = False
    alnum = False
    password = input("Enter a password between 6 and 12 characters: ")
    while len(password) < 6 or len(password)>12:
        if len(password)<6:
            print("your password is too short")
        elif len(password) > 12:
            print("your password is too long")
        password = input("Please enter a password between 6 and 12 characters.")

    for i in range(0,len(password)):
        if password[i].isupper():
            upcase = True
        if password[i].islower():
            lowcase = True
        if password[i].isalnum():
            alnum = True
        if password[i].isdigit():
            digits = True

    if digits and alnum and lowcase and upcase and digits:
       print("Your password is strong")
       notStrong = False
    elif (digits and alnum) or (digits and lowcase) or (digits and upcase) or (alnum and lowcase) or (alnum and upcase) or (lowcase and upcase):
        print("Your password is medium strength")
    else:
        print("Your password is weak")
user14678216
  • 1,412
  • 1
  • 6
  • 23
0

You don't have to make use of a loop, you can simply call the function within itself if the password is weak or medium, like so:

def Password():
    
    digits = False
    upcase = False
    lowcase = False
    alnum = False
    password = input("Enter a password between 6 and 12 characters: ")
    while len(password) < 6 or len(password)>12:
        if len(password)<6:
            print("your password is too short")
        elif len(password) > 12:
            print("your password is too long")
        password = input("Please enter a password between 6 and 12 characters.")

    for i in range(0,len(password)):
        if password[i].isupper():
            upcase = True
        if password[i].islower():
            lowcase = True
        if password[i].isalnum():
            alnum = True
        if password[i].isdigit():
            digits = True

    if digits and alnum and lowcase and upcase and digits:
        print("Your password is strong")
    elif (digits and alnum) or (digits and lowcase) or (digits and upcase) or (alnum and lowcase) or (alnum and upcase) or (lowcase and upcase):
        print("Your password is medium strength")
        Password()
    else:
        print("Your password is weak")
        Password()
        
Password()

This is a very useful technique and can be used to simplify many problems without the need for a loop.

Additionally, you can put in another check so that you can adjust the required strength of the password as needed in the future - this is not as easy to do (or rather, cannot be done as elegantly) if you use a while loop.

EDIT - To add to the advice given in the comments:

How does a "stack overflow" occur and how do you prevent it?

Not deleting the answer because I now realize that it serves as an example of how one shouldn't approach this question, and this may dissuade people from making the same mistake that I did.

ChaddRobertson
  • 366
  • 4
  • 14
  • IMO this is a poor programming practice because you're effectively make the function recursive. Besides using up stack space on each call, things can get tricky if the function is returns a value or doesn't non-trivial things after the call to itself. – martineau Nov 22 '20 at 18:14
  • Thanks for the heads up - I had no idea of this. Have edited my answer to include more information on the issue that you raised with it. – ChaddRobertson Nov 22 '20 at 18:32