如何使用 2 个随机小写字母、大写字母、数字和标点符号构建密码?

How can I build a password with 2 random lowercase, uppercase, numbers and punctuation?

我想用 Python 构建一个强密码生成器,强密码是 2 个小写字符、2 个大写字符、2 个数字、2 个符号。强大的部分给我错误。 while

中随机方法调用的位置参数过多
# Password generator
import random
import string


def create_waek_pass():
    password = random.randint(10000000, 99999999)
    print(f"password : {password}")


def create_strong_pass():
    password = []
    for i in range(2):
        lower = [string.ascii_lowercase]  # i wanted to create a list with 2 lower case chars
        upper = [string.ascii_uppercase] 
        number = [random.randint(0, 9)]
        exclimations = [string.punctuation]

    while len(password) <= 8:
        password = random.choice(lower, upper, number, exclimations)
    print(password)

正如我在评论中所说,发挥自己的安全功能从来都不是一个好主意,因为安全是一个复杂的问题 space,应该留给专业人员。但是你说这只是为了你自己的培训/学习所以下面是你的代码示例但修改后可以工作。这绝不是一个深思熟虑的设计,我只是采用了你的代码并使其工作。

# Password generator
from random import shuffle, choice
import string

def create_strong_pass():
    lower = string.ascii_lowercase
    upper = string.ascii_uppercase
    number = string.digits
    punctuation = string.punctuation
    password = []
    for _ in range(2):
        password.append(choice(lower))
        password.append(choice(upper))
        password.append(choice(number))
        password.append(choice(punctuation))
    shuffle(password)
    return "".join(password)

for _ in range(10):
    print(create_strong_pass())

输出

b7B#eR?7
)V2be7!Y
3Hng7_;V
q\/mDU74
Ii03/tW:
0Md6i;K@
<:LHw0b6
2eoM&V`6
c09N)Za(
t:34T'Bo