简体   繁体   中英

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

I want to build a strong password generator with Python and strong password would be 2 lowercase chars, 2 uppercase chars, 2 numbers, 2 symbols. The strong part gives me error. Too many positional arguments for method call on random in the 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)

As i said in my comment its never a good idea to role your own security functions as security is a complex space and should be left to professionals. however you said this is just for your own training / learning so below is an example of your code but modified to work. This is by no means a well thought design, i have simply taken your code and made it work.

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

OUTPUT

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM