python学习day4作业

# _*_ coding:utf-8 _*_
#coding mr.ding

"""
需求:
1),启动程序,首页面应该显示成如下格式:
欢迎来到博客园首页
1:请登录
2:请注册
3:文章页面
4:日记页面
5:评论页面
6:收藏页面
7:注销
8:退出程序
2),用户输入选项,3~6选项必须在用户登录成功之后,才能访问成功。
3),用户选择登录,用户名密码从register文件中读取验证,三次机会,没成功则结束整个程 序运行,成功之后,可以选择访问3~6项,
访问页面之前,必须要在log文件中打印日志, 日志格式为-->用户:xx 在xx年xx月xx日 执行了 %s函数,访问页面时,页面内容为:欢 迎xx用户访问评论(文章,日记,收藏)页面
4),如果用户没有注册,则可以选择注册,注册成功之后,可以自动完成登录,然后进入首页选择。
5),注销用户是指注销用户的登录状态,使其在访问任何页面时,必须重新登录。
6),退出程序为结束整个程序运行。
"""

import time



status_dict={
'username':None,
'status':False,
}


def wrapper_log(l): #日志装饰器,用于打印用户访问3~6功能日志。
def inner_log(*args,**kwargs):
re = l(*args,**kwargs)
while True:
with open("log", encoding="utf-8", mode="a") as f2:
f2.write("\n用户: %s 在 %s 执行了 %s 函数,访问页面时,页面内容为:欢迎%s用户访问 %s" % \
(status_dict["username"], time.strftime("%Y-%m-%d %H:%M:%S"),l.__name__,status_dict["username"],choice_dict[choice][0]))
return True
return re
return inner_log

def wrapper_login(loging): #登陆认证装饰器,用于如果用户没有登陆选择3~6功能会提醒用户进行登陆。
def inner_loging(*args,**kwargs):
reg = loging(*args, **kwargs)
if status_dict["username"] is not None and status_dict["status"] == True:
print("欢迎"+status_dict["username"]+"用户,访问"+choice_dict[choice][0]+"")
else:
print("您还没有登陆,不能使用此模块。")
return False

return reg
return inner_loging




def login(): #登陆函数
i = 0
while i < 3:
username = input('请输入用户名:').strip()
password = input('请输入密码:').strip()
if status_dict["username"] is not None and status_dict["status"] == True:
print("用户"+status_dict["username"]+"已登陆,请注销后重新登陆。")
break
with open('register',encoding='utf-8') as f1:
for line in f1:
line_list = line.strip().replace(',', ',').split(',')
if username == line_list[0] and password==line_list[1]:
print('登录成功...')
status_dict['username'] = username
status_dict['status'] = True
return True
else:
print('账号或者密码不正确,请重新输入')
i += 1
if i == 3:
exit()






def regiter(*args,**kwargs): #注册函数
while True:
username = input('请输入注册用户名:').strip()
password = input('请输入用户名密码:').strip()
with open('register',encoding='utf-8') as f1:
for line in f1:
line_list = line.strip().replace(',', ',').split(',')
if username == line_list[0]:
print('用户名存在,请重新输入!')
break
else:
with open('register',encoding='utf-8',mode='a') as f2:
f2.write('\n{},{}'.format(username,password))
print('注册成功')
return True

#调用装饰器
@wrapper_login
@wrapper_log
def article_page(): #文章页面函数
print("请编辑文章。")

@wrapper_login
@wrapper_log
def diary_page(): #日志页面函数
print("请编辑日志。")




@wrapper_login
@wrapper_log
def comment_page(): #评论页面函数
print("请评论。")



@wrapper_login
@wrapper_log
def collection_page(): #收藏页面函数
print("请收藏")



def logout(): #注销函数
print("确认注销%s用户?输入y注销用户,输入n返回。" % status_dict["username"])
while True:
user_choice = input("请输入选项:")
if user_choice == "y":
status_dict["username"] = None
status_dict["status"] = False
print("注销成功,已返回登陆界面。")
login()
return True
elif user_choice == "n":
return False
else:
print("您输入的选项不正确,请重新输入。")




def exit_program(): #退出程序函数
print("确认要退出程序?输入y退出,输入n返回。" % status_dict["username"])
while True:
user = input("请输入选项:")
if user == "y":
print("退出程序成功。")
exit()
elif user == "n":
return False
else:
print("您输入的选项不正确,请重新输入。")




choice_dict = {
1: ['登录', login],
2: ['注册', regiter],
3: ['文章页面',article_page],
4: ['日记页面', diary_page],
5: ['评论页面', comment_page],
6: ['收藏页面', collection_page],
7: ['注销', logout],
8: ['退出程序',exit_program],
}



while True:
print("欢迎来到博客园")
for (k, v) in choice_dict.items():
print(k, v[0])

choice = input("请输入选项:")
if choice.isdigit():
choice = int(choice)
if 0 < choice< len(choice_dict)+1:
choice_dict[choice][-1]()
continue
else:
print("超出范围,请重新输入!")

猜你喜欢

转载自www.cnblogs.com/dingyang-python/p/9117659.html
今日推荐