码迷,mamicode.com
首页 > 其他好文 > 详细

用装饰器做一个登陆功能(进阶):

时间:2018-04-03 20:10:04      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:encoding   input   ret   pass   循环   lin   inpu   color   list   

 1 dic = {
 2     username:None,
 3     status:False
 4 }
 5 def login(flag):     #  传入 ‘微信‘,‘QQ‘
 6     def wrapper(f):
 7         def inner(*args,**kwargs):
 8             if dic[status] == True:
 9                 ret = f()
10                 return ret
11             else:
12                 i = 0
13                 while i < 3:
14                     username = input(请输入你的%s账号: % (flag)).strip()
15                     password = input(请输入你的%s密码: % (flag)).strip()
16                     with open(login_msg,encoding=utf-8) as f1:
17                         content = eval(f1.readline())   #eval() 将文件中的内容直接取出赋值给content
18                         if username == content[flag][username] and password == content[flag][password]:  #falg 的目的就是接受传入的内容是‘微信‘还是‘QQ‘
19                             print(登陆成功)
20                             dic[username] = username
21                             dic[status] = True   #改变状态
22                             ret = f()
23                             return ret
24                         else:
25                             print(你输入的账号或密码错误,你还有%d次机会,请重新输入... % (2-i))
26                             i += 1
27         return inner
28     return wrapper
29 @login(微信)
30 def taobao_home():
31     print(淘宝首页)
32 @login(微信)
33 def taobao_shop():
34     print(淘宝商城)
35 @login(qq)
36 def jingdong_home():
37     print(京东首页)
38 @login(qq)
39 def jingdong_shop():
40     print(京东商城)
41 
42 choice_list = {          #定义一个choice_list 用数字作key 函数名作值。
43     1:taobao_home,
44     2:taobao_shop,
45     3:jingdong_home,
46     4:jingdong_shop
47 }
48 
49 while True:
50     print(1\t淘宝首页\n2\t淘宝商城\n3\t京东首页\n4\t京东商城)   #循环打印四个内容
51     choice = input(请输入你的选项:).strip()
52     if choice.isdigit():
53         choice = int(choice)
54         if  0 < choice <= len(choice_list):
55             choice_list[choice]()   #  通过选择的数字 执行相应的函数
56         else:
57             print(你输入的不在选项范围内,请重新输入...)
58     else:
59         print(你输入的不是数字,请重新输入...)

 

 

用装饰器做一个登陆功能(进阶):

标签:encoding   input   ret   pass   循环   lin   inpu   color   list   

原文地址:https://www.cnblogs.com/stfei/p/8710785.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!