所以我想写一个 Python 代码,为用户创建一个随机密码。它需要询问用户他们希望密码多长,并且有最少的字符集。
几个星期以来,我一直在努力寻找一些东西,但就是找不到,如果这个问题已经解决了,我深表歉意。
这是我到目前为止的代码,不知道我需要什么其他代码以及我必须在哪里使用才能获得我想要的东西。
import random
import secrets
import string
# This variable includes numbers
digits = string.digits
# This variable includes uppercase and lowercase letters
letters = string.ascii_lowercase
# This variable includes symbols
symbols = "!#$%&'()*+, -./:;<=>?@[\]^_`{|}~"
password = ''.join(secrets.choice(digits + letters + symbols) for t in range(20))
def message():
print("Hello, here is a password generator: ")
print("Choose the length?")
print(userGen())
def userGen():
upper = int(input("How many letters you want? "))
spec = int(input("How many symbols you want? "))
num = int(input("How many numbers you want? "))
return passGen(upper,spec,num)
def passGen(upper,spec,num):
new_password = ""
for i in range(upper):
new_password += random.choice(letters)
for x in range(spec):
new_password += random.choice(symbols)
for y in range(num):
new_password += random.choice(digits)
pass_word = list(new_password)
shuff = random.shuffle(pass_word)
new_pass = "".join(pass_word)
password = secrets.token_urlsafe(7)
return new_pass
message()
繁星coding
红颜莎娜
江户川乱折腾
忽然笑
相关分类