My code works fine but I definitely have unecessary repeat code. I'd like to use functions so I don't have repeat code. I would appreciate any advise on how to make my password generator code more concise.
I've tried looking through other forums but I couldn't find the answer I'm looking for.
import string
import random
lowerCase = string.ascii_lowercase[:26]
upperCase = string.ascii_uppercase[:26]
numberList = ["1","2","3","4","5","6","7","8","9"]
specialCharacters = ["*","&","@","!"]
responseList = ["weak","medium","strong"]
randomCharacters =[ ]
i=0
while True:
choice = input("Want to create a password? To make your choice for your password strength, please respond with one of the following: weak/medium/strong. ")
if choice in responseList:
if choice == "weak":
while i<=5:
randomLetterLower = random.choice(lowerCase)
randomCharacters.append(randomLetterLower)
i += 1
elif choice == "medium":
while i<=4:
randomLetterLower = random.choice(lowerCase)
randomCharacters.append(randomLetterLower)
randomLetterUpper = random.choice(upperCase)
randomCharacters.append(randomLetterUpper)
i += 1
elif choice == "strong":
while i<=1:
randomLetterLower = random.choice(lowerCase)
randomCharacters.append(randomLetterLower)
randomLetterUpper = random.choice(upperCase)
randomCharacters.append(randomLetterUpper)
randomNumber = random.choice(numberList)
randomCharacters.append(randomNumber)
randomSpecialCharacter = random.choice(specialCharacters)
randomCharacters.append(randomSpecialCharacter)
i += 1
output = " ".join(randomCharacters)
print(output)
i = 0
randomCharacters =[]
else:
break
secrets
module for cryptographically secure random character selection;random
was not designed for cryptographically secure random generation. \$\endgroup\$