GeorgeOke24
GeorgeOke24

Reputation: 33

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()

Upvotes: 2

Views: 149

Answers (2)

GeorgeOke24
GeorgeOke24

Reputation: 33

This is the corrected code:

    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)



        list_chars = list(chars)
        random.shuffle(list_chars)
        pwd = "".join(list_chars)

        print(pwd)

    password()

I replaced:

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

with

    list_chars = list(chars)
    random.shuffle(list_chars)
    pwd = "".join(list_chars)

Putting the altered chars variable in a list allowed me to shuffle it, randomizing it and allowing me to assign it to pwd

Upvotes: 1

luigibertaco
luigibertaco

Reputation: 1132

I think the last part is what is confusing you. You are building the chars variable with the correct amount of specific chars, but you then choose between them again at the end.

You could just change:

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

With

# option 1 - works better if working with lists
list_chars = list(chars)
random.shuffle(chars)
password = "".join(list_chars)

# option 2 - simpler solution for strings
password = "".join(random.sample(char, len(char)))

You could also use shuffle to select the chars before without the for loops, something like:

# for this to work your `charslet` must be a list
random.shuffle(charslet)
chars += "".join(charslet[:passlet])

Upvotes: 3

Related Questions