Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

Password Generator - implementation with minimum password length constraint e.g. password length = 8 (in python)

I am not able to modify the code when I implement minimum password length like minimum length must be 8 I tried using a while loop but code is not running as expected Please help me in this case

       import random
       letters = ['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', 
        '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']
       numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
       symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

       print("Welcome to the PyPassword Generator!")
       nr_letters= int(input("How many letters would you like in your 
       password?
")) 
       nr_symbols = int(input(f"How many symbols would you like?
"))
       nr_numbers = int(input(f"How many numbers would you like?
"))

       password = []
       password.extend(random.sample(letters, nr_letters))
       password.extend(random.sample(symbols, nr_symbols))
       password.extend(random.sample(numbers, nr_numbers))
       random.shuffle(password)
       finalPassword = ""
       print(f"Here is you password: {finalPassword.join(password)}")
question from:https://stackoverflow.com/questions/65625866/password-generator-implementation-with-minimum-password-length-constraint-e-g

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Simply keep the input statement within a while loop that checks your criteria:

   import random
   letters = ['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', 
    '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']
   numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
   symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

   print("Welcome to the PyPassword Generator!")

   nr_letters = 0
   nr_symbols = 0
   nr_numbers = 0
   while nr_letters + nr_symbols + nr_numbers < 8:
       nr_letters= int(input("How many letters would you like in your password?
")) 
       nr_symbols = int(input(f"How many symbols would you like?
"))
       nr_numbers = int(input(f"How many numbers would you like?
"))

   password = []
   password.extend(random.sample(letters, nr_letters))
   password.extend(random.sample(symbols, nr_symbols))
   password.extend(random.sample(numbers, nr_numbers))
   random.shuffle(password)
   finalPassword = ""
   print(f"Here is you password: {finalPassword.join(password)}")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...