third作业--博客园登录

  1 import time
  2 status_dic = {
  3     'username': None,
  4     'status': False,
  5 }
  6 
  7 flag1 = True
  8 
  9 def login(*args,**kwargs):
 10     i = 0
 11     while i < 3:
 12         username = input('请输入用户名:').strip()
 13         password = input('请输入密码:').strip()
 14         with open('register',encoding='utf-8') as f1:
 15             for line in f1:
 16                 line_list = line.strip().split()
 17                 if username == line_list[0] and password == line_list[1]:
 18                     print('登录成功')
 19                     status_dic['username'] = username
 20                     status_dic['status'] = True
 21                     return True
 22             else:
 23                 print('输入不正确,请重新输入,还剩%s机会' % (2-i))
 24                 if i == 2: return Quit()
 25             i += 1
 26 
 27 
 28 def register(*args, **kwargs):
 29     flag = True
 30     while flag:
 31         username = input('请输入要注册的用户名:')
 32         f1 = open('register',encoding='utf-8')
 33         for i in f1:
 34             if username in i:
 35                 print('用户名重复,请重新输入')
 36                 f1.close()
 37                 break
 38         else:
 39             f1.close()
 40             password = input('请输入要注册的密码:').strip()
 41             f2 = open('register', encoding='utf-8', mode='a')
 42             f2.write('\n{}\t{}'.format(username, password))
 43             f2.close()
 44             print('恭喜你,注册成功,已经自动为您登录.....')
 45             status_dic['username'] = username
 46             status_dic['status'] = True
 47             return True
 48 
 49 
 50 def wrapper(func):
 51     def inner(*args,**kwargs):
 52         if status_dic['status']:
 53             ret = func(*args,**kwargs)
 54             return ret
 55         else:
 56             print('请先进行登录')
 57             if login():
 58                 ret = func(*args, **kwargs)
 59                 return ret
 60     return inner
 61 
 62 def log_record(func):
 63     def inner(*args,**kwargs):
 64         struct_time = time.localtime()
 65         time_now = time.strftime("%Y-%m-%d %H:%M:%S", struct_time)
 66         with open('log_func','a',encoding='utf-8') as f1:
 67             f1.write('用户:%s 在%s 执行了 %s函数\n'%(status_dic['username'],time_now,func.__name__))
 68         ret = func(*args,**kwargs)
 69         return ret
 70     return inner
 71 
 72 @wrapper
 73 @log_record
 74 def article():
 75     print('欢迎%s访问文章页面' % status_dic['username'])
 76 
 77 @wrapper
 78 @log_record
 79 def diary():
 80     print('欢迎%s访问日记页面' % status_dic['username'])
 81 
 82 @wrapper
 83 @log_record
 84 def comment():
 85     print('欢迎%s访问评论页面' % status_dic['username'])
 86 
 87 @wrapper
 88 @log_record
 89 def enshrine():
 90     print('欢迎%s访问收藏页面' % status_dic['username'])
 91 
 92 
 93 def login_out():
 94     status_dic['username'] = None
 95     status_dic['status'] = False
 96     print('注销成功')
 97 
 98 def Quit():
 99     global flag1
100     flag1 = False
101     return flag1
102 
103 choice_dict = {
104     1: login,
105     2: register,
106     3: article,
107     4: diary,
108     5: comment,
109     6: enshrine,
110     7: login_out,
111     8: Quit,
112 }
113 
114 
115 while flag1:
116     print('欢迎来到博客园首页\n1:请登录\n2:请注册\n3:文章页面\n4:日记页面\n5:评论页面\n6:收藏页面\n7:注销\n8:退出程序')
117     choice = input('请输入您选择的序号:').strip()
118     if choice.isdigit():
119         choice = int(choice)
120         if 0 < choice <= len(choice_dict):
121                 choice_dict[choice]()
122         else:
123             print('您输入的超出范围,请重新输入')
124 
125     else:
126         print('您输入的选项有非法字符,请重新输入。')

猜你喜欢

转载自www.cnblogs.com/lijie123/p/9022365.html