# -*- coding:utf-8 -*-
import time
login_state = False
user_dict = {'username': None}
def register(): # 注册函数
while True:
username = input("请输入注册账号:").strip()
password = input("请输入注册密码:").strip()
with open("register", encoding="UTF-8")as reg:
for i in reg:
list_reg = i.strip().split(',')
if username == list_reg[0]:
print("用户名已经存在,请重新输入")
break
else:
with open("register", encoding="UTF-8", mode="a")as ligo:
ligo.write('\n{},{}'.format(username, password))
print("注册成功")
return True
def login(): # 登陆函数
global login_state
global user_dict
i = 0 # 计数器
while i < 3: # 超过3次后,登陆失败
username = input("请输入您的账号:").strip()
password = input("请输入您的密码:").strip() # 去除空格及换号符
with open('register', encoding='UTF-8')as f1:
for line in f1: # 循环读取注册文件中的内容
line_list = line.strip().split(',')
if username == line_list[0] and password == line_list[1]:
print("*******登陆成功*******")
login_state = True
user_dict = line_list[0]
return True
else:
print("账户或密码输入错误")
i += 1
def log(e):
def loge():
log_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
with open("log", encoding="UTF-8", mode="a") as logg:
logg.write('\n{},{}登陆了,执行了{}函数'.format(log_time, user_dict, e.__name__))
e()
return loge
def jian_ce(f): # 认证函数,检测用户是否登陆
def inner():
global login_state
if login_state == False:
print("您尚未登陆,请先登陆后在执行程序")
login()
else:
f()
return inner
def logoff(): # 注销函数
global login_state
if login_state == True:
login_state = False
print("注销成功")
return login_state
else:
print("您尚未登陆,不需要注销")
@jian_ce # 语法糖,装饰器
@log
def wen_zhang():
print("这是文章页面")
@jian_ce # 语法糖,装饰器
@log
def ri_ji():
print("这是日记页面")
@jian_ce # 语法糖,装饰器
@log
def ping_lun():
print("这是评论页面")
@jian_ce # 语法糖,装饰器
@log
def shou_cang():
print("这是收藏页面")
xu_dict = { # 定义了一个字典,存放序列号对应函数
1: login,
2: register,
3: wen_zhang,
4: ri_ji,
5: ping_lun,
6: shou_cang,
7: logoff
}
while True:
print('''-------欢迎来到博客园-------
1:请登陆
2:请注册
3:文章页面
4:日记页面
5:评论页面
6:收藏页面
7:注销
8:退出程序
''')
xu_hao = input("请输入序列号:").strip()
if xu_hao.isdigit():
xu_hao = int(xu_hao)
if xu_hao > 0 and xu_hao <= len(xu_dict):
xu_dict[xu_hao]()
elif xu_hao == 8:
break
print("再见!退出程序成功")
else:
print("你输入的序号不存在")
else:
print("您输入的序列号存在非法字符")
原文地址:http://blog.51cto.com/11258494/2122229