1 def add_func(func): 2 def wrapper(*args, **kwargs): 3 username = input("亲,请输入你的用户名: ").strip() 4 passwd = input("请输入密码").strip() 5 if username == 'sb' and passwd == '123': 6 res = func(*args, **kwargs) 7 return res 8 else: 9 print("您输入的用户名或者密码错误") 10 11 return wrapper 12 @add_func 13 def index(): 14 print('欢迎来到京东商城') 15 @add_func 16 def home(name): 17 print("%s 欢迎回到主页" % name) 18 @add_func 19 def shopping_car(name): 20 print('%s的购物车里面有[手机、电脑、汽车]'%name) 21 index() 22 home('liyulu') 23 shopping_car('liyulu') 24 输出: 25 亲,请输入你的用户名: sb 26 请输入密码123 27 欢迎来到京东商城 28 亲,请输入你的用户名: sb 29 请输入密码123 30 liyulu 欢迎回到主页 31 亲,请输入你的用户名: sb 32 请输入密码123 33 liyulu的购物车里面有[手机、电脑、汽车]