import string
FILE_NAME = "user.txt" #常量
def get_all_user():
users = {}
with open(FILE_NAME,‘a+‘,encoding="utf-8") as fr:
fr.seek(0)
for line in fr: #xiaohei,123456
if line.strip(): #是否为空行
username, password = line.strip().split(‘,‘)
users[username] = password
return users
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 len(password)>=8 and len(password)<=12
def check_username(username):
return len(username)>=6 and len(username)<=12
def write_user(username,password):
with open(FILE_NAME, ‘a+‘, encoding="utf-8") as fw:
fw.write(username+‘,‘+password+‘\n‘)
for i in range(3):
username = input("username:").strip()
password = input("password:").strip()
cpwd = input("cpwd:").strip()
if not check_username(username):
print("用户名必须长度必须6-12")
continue
if not check_password(password):
print("密码必须长度必须8-12,必须包含大小写字母、数字")
continue
if password != cpwd:
print("两次输入密码不一致")
continue
all_user = get_all_user()
if username in all_user:
print("用户已经存在")
else:
write_user(username,password)
print("注册成功!")
break
else:
print("错误次数过多,最多可以输入3次")
原文:https://www.cnblogs.com/zhangmeiyan/p/14298638.html