Trying to create a Password Generator that can save passwords

122 Views Asked by At

I have created the baseline of the program, and it works well, but I want it to be able to save the password generations, and store them under an index name in SQLite. I want it to be so that I can always access the previously generated passwords.

Code:

    def gen_password():
        letters = string.ascii_letters
        random_letter = random.choice(letters)
        random_num = random.randint(0,9)
        letter_or_num = ["letter","number"]
        char_num = int(input("How many characters do you want in your password?: "))
        for i in range(char_num):
            random_var = random.choice(letter_or_num)
            if random_var == "letter":
                random_letter = random.choice(letters)
                print(random_letter, end = "")
            elif random_var == "number":
                random_num = random.randint(0,9)
                print(random_num, end = "")
        print("")
    
    gen_password()
1

There are 1 best solutions below

2
On

I hope this will solve your problem:

def gen_password():
    letters = string.ascii_letters
    random_letter = random.choice(letters)
    random_num = random.randint(0,9)
    letter_or_num = ["letter","number"]
    char_num = int(input("How many characters do you want in your password?: "))
   
    password = []
    for i in range(char_num):
        random_var = random.choice(letter_or_num)
        if random_var == "letter":
            random_letter = random.choice(letters)
            # replace print with this:
            password.append(random_letter)  
         
        elif random_var == "number":
            random_num = random.randint(0,9)
            password.append(str(random_num))
    return "".join(password)

print(gen_password())