DoABarrelRoll94
DoABarrelRoll94

Reputation: 23

Printing empty list

def password_generator():           # generates a password

    chars = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
    password = ''
    strength = input("What would you like your password strength to be?(weak/medium/strong) ")

    if strength == "weak":

        letters = [elem for elem in chars.split() if chars.isalpha()] 
        print(letters)
        #for elem in range(10):

         #   password += random.choice(letters)

    return password

Why does 'letters' come out to be empty? In the line before print(letters), I assign letters only the alphabetic characters from chars, maybe this is the wrong syntax.

Upvotes: 0

Views: 45

Answers (2)

Milo Bem
Milo Bem

Reputation: 1033

if you only want alpha characters in letters why not just do this?

letters = string.ascii_lowercase + string.ascii_uppercase

Upvotes: 0

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

that

letters = [elem for elem in chars.split() if chars.isalpha()] 

makes no sense at all. chars.split() returns a list with chars as sole element...

And your condition chars.isalpha() is false: it applies to the whole character set...

You probably meant:

letters = [elem for elem in chars if elem.isalpha()] 

If you want letters to be able to perform a random on them you can just do:

letters = string.ascii_letters

demo:

>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

Upvotes: 3

Related Questions