打印空清单

时间:2018-11-13 16:27:39

标签: python-3.x

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

为什么“字母”为空?在print(letters)之前的行中,我仅分配了chars中的字母字符,这可能是错误的语法。

2 个答案:

答案 0 :(得分:3)

那个

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

毫无意义。 chars.split()返回一个列表,其中以chars作为唯一元素...

您的条件chars.isalpha()为假:它适用于整个字符集...

您可能是说:

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

如果您希望字母能够对它们执行random,则可以执行以下操作:

letters = string.ascii_letters

演示:

>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

答案 1 :(得分:0)

如果您只想字母中的字母字符,为什么不这样做呢?

LOAD DATA INPATH