2020-12-31

How to validate user input while running a while with those values

I'm writing a password generator that asks the user to enter a password LENGHT and generates it that long (it works)

Problem: I do validate user input (while/try/except), it gets the Value Error right, it gets the minimum length right BUT it does not take the maximum value and generates always the SAME password with the length PLUS the new length.

Maybe some examples will help

from random import randint

lowerCase = "abcdefghijklmnopqrstuvwxyz"
upperCase = lowerCase.upper()
numbers = "1234567890"
special = "!#$%&/()=?¡@[]_<>,."

password_creation = lowerCase + upperCase + numbers + special

password = ""
lenght = 0
while True
    try:
        password_lenght = int(input("How many characters do you wish the password? Minimum 8 maximum 1024: "))
        if password_lenght < 8 or password_lenght > 1024:
            print("Minimun 8 characters - maximum 1024, try again")
    except ValueError:
        print ("Please enter a valid NUMBER...")
        continue
    else:
        while lenght < password_lenght:



        password = password + password_creation[randint(0, len(password_creation) -1)]
        lenght +=1
              
    
        print("Generated password: ", password)
    

if I run this code it kind of works:

>>> %Run z_strong_password_gen.py
How many characters do you wish the password? Minimum 8 maximum 1024: asdsads
Please enter a valid NUMBER...

ok, it detects if I enter non-numerical values,

now let's try a number below 8 (the minimum.

How many characters do you wish the password? Minimum 8 maximum 1024: 1
Minimun 8 characters - maximum 1024, try again
Generated password:  $
How many characters do you wish the password? Minimum 8 maximum 1024: 3
Minimun 8 characters - maximum 1024, try again
Generated password:  $?$)

ok, it goes with the message "Minimun 8 characters - maximum 1024, try again" BUT it calculates the password

now let's try with the maximum

How many characters do you wish the password? Minimum 8 maximum 1024: 1030
Minimun 8 characters - maximum 1024, try again
Generated password:  $?$NDcB&vMDY&IX@¡x#hLxP8u!oa_F6PGg&DQV8WBOwkqJW@VJ8nmFA$wrF]7soN<C(J[x#[HRm,xL(7(OiCcdV43#XUfa9o7LYwBq55[n24geBl=@U<&!$fH]01&>]bG3/l&[>u2mA4N9IiNUUTj=tWJ)g4wz>A67qNV7O]aKZVfvc3F?B/TCsCjzYe3pOQ%f1nrvFO7prBr[C_Eqs8W?7GU/vor/>xJtV.FLKUL94)KZ(JCc1#gFv,=wJ5rvBR2[5)hhv[tAi)&8AXB1n)FPyUi6WZeqzWnj@R0t.WYja%r2pD_3¡hoW7nRncdf,ExV5._)W(/P3yOyvp#jZNpQIt6Y1cbfoq[mweSp#f)¡ADNz_Duibe<U9pJD4]4x,/G51fORryfVaekX=PIes?T>KTvfy>sQMgNjZ]4M,=5vD!G%,/gYZn(J_pxJlKzjUE65VJn)ZRVpI!&dHDtK(vh=IN=p4i7#E¡4[r!.$]J@>4&HQqSq?DXyvis<Gs)@$=5I/SRGV2&¡]DU0P$=CF,hoj7@]KhPLWsWrFgC@nEU8i3jC3yZKt)_r=m?@KH<sA$D#,f_2%rV$gwzMZoyxh=b[kBD5N%k<1SEpc<f93.)9eh74D6oi)XkylTNA3O%Vq)eUpF9VA$H5)NYUBHihMW.$O#ytgb!24?WLzi>piF0T(!6V#l1141v@i%]_BxCV2uPEPdajOdxqkJs#Fq,zAa#T.%DE%D%,I<y4zY5lgBcua!,LBu1jHf%4waKujW.Y&WMpnf<qa%#sCZlGm17iNu9fRLXXAdO[1s@g4?ZhTL60Blg?PtZNJw<AB>$poCK¡cJ7ON$kXAEZ/c¡X/H]FCHhjCVUm#na¡p_o>]7Bp//rr3YJFj<OpCL2kg,,XEsC0¡<i/$HP#Bc!X#nMlRVMW8NdV7x7ru_(2OW6/Q1P¡c_ZmJz<Z<7zELz¡6yp00$ZD&sErhSz81mVU$oO(rtqAYs=p3h%lbTr$EWiJxvq)X<z909k18rN07ZRfq@C7hWRyTH1!?&PR3%@?
How many characters do you wish the password? Minimum 8 maximum 1024: 

It raises the message that the number is bigger than 1024 BUT it calculates the password again

And if I do calculate a password, let's say, 10 characters..it does ok, if I want to calculate another one of...20, it takes the first 10 from the previous calculation and just add 10 more to fill the 20 (maybe I have to clear or delelte the varaiable)

check the example

How many characters do you wish the password? Minimum 8 maximum 1024: 10
Generated password:  **fOweHv%Wm,**
How many characters do you wish the password? Minimum 8 maximum 1024: 20
Generated password:  **fOweHv%Wm,**p6AEoh8L)l
How many characters do you wish the password? Minimum 8 maximum 1024:


from Recent Questions - Stack Overflow https://ift.tt/37YT68j
https://ift.tt/eA8V8J

No comments:

Post a Comment