'Random password generator with words limit

I am creating a random password generator. I wanted to create an input field where the length of the password can only be min 8 and max 16. If user input the length other than the allowable length, it will keep asking the user to input the right password length. I use 'return' so it return to length = int(input('\nEnter the length of password: ')) , but it does not work here. Can I get advice on how to make this right?

#input the length of password
#must be minimun 8 to maximun 16

length = int(input('\nEnter the length of password: '))

#if use enter less than 8 or more than 16 

if length < 8 :
        print('You password length is too short(must be more than 8 character)')
        return;
elif length > 16:
        print('You password length is too long(must be less than 17 character)')
        return;
else:
        print('You password length looks good') 
    


Solution 1:[1]

Use the built-in len function instead and use a while loop:

#input the length of password
#must be minimun 8 to maximun 16

#if use enter less than 8 or more than 16
l = False
while not l:
    length = input('\nEnter the length of password: ')
    if len(length) < 8 :
        print('You password length is too short(must be more than 8 character)')
        print(len(length), "is the length of your password")
    elif len(length) > 16:
            print('You password length is too long(must be less than 17 character)')
            print(len(length), "is the length of your password")
    else:
            print('You password length looks good')
            break
        

Output:

Enter the length of password: thisismorethan16characters
You password length is too long(must be less than 17 character)
26 is the length of your password

Enter the length of password: thisisgood
You password length looks good
>>> 

Also, remove the int() so we can allow the user to enter a string, and you usually use return when it's in a function. Also there is no use of ; in python.

Solution 2:[2]

the code will run till you give a correct password

the code will run till you enter a correct password. I declared a variable correct_pass set to false. with condition correct_pass == False in while loop when the user enters correct password the value of correct_pass is changed to True and hence it exits out of the loop and in other cases it value remains unchanged

correct_pass=False

while(correct_pass==False):
      length = input('\nEnter the length of password: ')
if len(length) < 8 :
    print('You password length is too short(must be more than 8 character)')
    correct_pass=False
elif len(length) < 8:
    print('You password length is too long(must be less than 17 character)')
    correct_pass=False
else:
    print('You password length looks good')
    correct_pass=True

Solution 3:[3]

Your code is basically using integer value instead of the length or the number of integers the user inputs.

let's say I enter 9 as the input.

Then, you're using an If-elif-else block.

if length (user input which is 9 at the moment) <8, print "Your password length is too short". In our case, the length is equal to 9 and it's also less than 17. So we satisfy the conditions.

But if we enter, for example0, 2222. This is obviously a much bigger value than what your code asked for. I hope I made your mistake clear. Now, the fix would be: Check out this comment

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Sheryar