python注册账号,python实战:注册新用户

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次")

标签:实战,username,set,python,user,注册,strip,print,password

来源: https://www.cnblogs.com/zhangmeiyan/p/14298638.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值