注册和登录代码

注册:

#1、先把文件内容读出来

user_info = {}
fw = open('users','a+',encoding='utf-8')
fw.seek(0)
for line in fw:
    line = line.strip()
    if line!='':
        username,password,error_count = line.split(',')
        user_info[username] = {'password':password,'error_count':int(error_count)}

for i in range(3):
    username = input('username:').strip().lower()
    password = input('password:').strip()
    cpassword = input('cpassword:').strip()
    if username=='' or password=='' or cpassword=='':
        print('账号?密码、确认密码不能为空')
    elif len(username)<6 or len(username)>12 or  len(password)<6 or len(password)>12:
        print('账号/密码的长度最小6位,最大12位')
    elif password !=cpassword:
        print('两次输入的密码不一致')
    elif username in user_info:
        print('用户名已经存在!')
    else:
        user_str = '%s,%s,0\n'%(username,password)
        fw.write(user_str)
        fw.flush() #
        user_info[username] = {'password': password, 'error_count': 0}
        print('注册成功!')
        # break
fw.close()

登录

# 2、登录 2py
# 1、账号、密码不能为空
#2、要校验账号是否存在,不存在要提示
#3、最多输入3次
#4、账号不区分大小
# 5、账号和密码长度要大于等于6,小于等于12
#6、每次登录的时候密码错误,就这个账号的后面加一个失败次数,
#7、如果失败次数大于3次的话,提示账号已经被锁定。
import datetime
user_info = {}
fw = open('users','a+',encoding='utf-8')
fw.seek(0)
for line in fw:
    line = line.strip()
    if line!='':
        username,password,error_count = line.split(',')
        user_info[username] = {'password':password,'error_count':int(error_count)}

for i in range(3):
    username = input('username:').strip().lower()
    password = input('password:').strip()
    if username=='' or password=='' :
        print('账号?密码不能为空')
    elif len(username)<6 or len(username)>12 or  len(password)<6 or len(password)>12:
        print('账号/密码的长度最小6位,最大12位')
    elif username not in user_info:
        print('用户名不存在!')
    elif user_info[username]['error_count'] >3:
        print('密码输入错误次数太多!')
    elif user_info.get(username).get('password') !=password:
        print('密码错误!')
        user_info[username]['error_count']+=1
    else:
        print('登录成功!%s'%datetime.datetime.now())
        break



fw.seek(0)
fw.truncate()



for username,user_dict in user_info.items():
    password= user_dict.get('password')
    error_count= user_dict.get('error_count')
    user_str = '%s,%s,%s\n'%(username,password,error_count)
    fw.write(user_str)
    fw.flush()

fw.close()

猜你喜欢

转载自www.cnblogs.com/Dorami/p/10967273.html