首页 > 编程语言 > 详细

python实战:注册新用户

时间:2021-01-19 20:00:33      阅读:36      评论:0      收藏:0      [点我收藏+]
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次")

python实战:注册新用户

原文:https://www.cnblogs.com/zhangmeiyan/p/14298638.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!