mardi 29 septembre 2020

How to specify variables with random in Python

I am creating a password generator the takes the length of the desired password, number of letters, as well as the number of numbers. The password needs to contain uppercase letters as well as numbers and special characters. I am having trouble figuring out how to specify the number of letters and numbers in the password. This is what I have so far:

import random

charslet ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
charsnum = "1234567890"
charssym = "!@#$%^&*"

def password():
    pwd = ""

    passlen = int(input("How long do you want the password to be?: "))
    passlet = int(input("How many letters do you want in your password?: "))
    passnum = int(input("How many numbers do you want in your password?: "))
    passsym = int(passlen - (passlet + passnum))
    chars = ""

    for let in range(passlet):
        chars += random.choice(charslet)
    for num in range(passnum):
        chars += random.choice(charsnum)
    for sym in range(passsym):
        chars += random.choice(charssym)



    for p in range(passlen):
        pwd += random.choice(chars)

    print(pwd)

password()




Aucun commentaire:

Enregistrer un commentaire