Python实战:登录系统

import string,datetime

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


for i in range(3):
username = input("username:").strip()
password = input("password:").strip()
if not check_username(username):
print("用户名必须长度必须6-12")
continue
if not check_password(password):
print("密码必须长度必须8-12,必须包含大小写字母、数字")
continue
all_user = get_all_user()
if username in all_user:
f_password = all_user.get(username)
if f_password == password:
print("登录成功!今天的日期是 %s" %datetime.datetime.today() )
break
else:
print("密码错误!")
else:
print("用户不存在")

else:
print("错误次数过多,最多可以输入3次")
posted @ 2021-01-19 16:29  Mia妈妈加油呀  阅读(118)  评论(0)    收藏  举报