Basically I am working on a random password generator and I wanted a way to add the passwords that are generated into a .txt file.
Below is my current code.
import random
letters = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") # The list of letters, numbers and special characters that will be used in the generation.
numbers = list("1234567890")
special_characters = list("!@#$%^&*()")
characters = list("letters" + "numbers" + "special_characters")
def generate_random_password():
length = int(input("Enter password length: "))
letter_count = int(input("How many letters do you want in your password? "))
number_count = int(input("How many numbers do you want? "))
special_character_count = int(input("How many special characters do you want? "))
character_count = letter_count + number_count + special_character_count
if character_count > length:
print(
"Character count surpasses the set length, please try again.") # Informs user that they made the total character count longer than the designated length for the password.
return
userpassword = []
for i in range(letter_count):
userpassword.append(random.choice(letters))
for i in range(letter_count):
userpassword.append(random.choice(numbers))
for i in range(special_character_count):
userpassword.append(random.choice(special_characters))
if character_count < length:
random.shuffle(characters)
for i in range(length - character_count):
userpassword.append(random.choice(characters))
random.shuffle(userpassword)
print("".join(userpassword))
generate_random_password() #The final command that generates the password
`