随机生成用户名和密码,需求如下

#账号和密码
#必须包含字母和数字,长度在6-12之间
#密码,必须包含大小写字母、数字、特殊符号,长度在8-12之间
#输入几,就产生几条,并且用户名不能重复
import string,random
number = input("请输入生成的条数:").strip()
# print(type(number))
if not number.isdigit():
print("请输入整数")
else:
number = int(number)

def check_username(username):
return set(username) & set(string.ascii_letters) and set(username) & set(string.digits)
def check_password(password):
return set(password) & set(string.ascii_uppercase) and set(password) & set(string.ascii_lowercase) and \
set(password) & set(string.digits) and set(password) & set(string.punctuation)
l = {}
count =0
while count<number:
username_length = random.randint(6,12)
username_list = random.sample(string.ascii_letters + string.digits,username_length)
if check_username(username_list):
username = ''.join(username_list)
else:
continue
password_length = random.randint(8,12)
password_list = random.sample(string.ascii_letters + string.digits + string.punctuation,password_length)
if check_password(password_list):
password = ''.join(password_list)
else:
continue
if username not in l:
count += 1
l[username] = password
print(l)
posted @ 2021-01-12 15:11  术成  阅读(881)  评论(0)    收藏  举报