users_dic = [{'name':'zsz','passwd':'123'},
{'name':'alex','passwd':'123'},
{'name':'lhf','passwd':'123'}]
current_dic = {"username":None,"Login":False}
def auth_func(func):
def wrapper(*args,**kwargs):
if current_dic["username"] and current_dic["Login"]:
res = func(*args,**kwargs)
return res
username = input("用户名:").strip()
passwd = input("密码:").strip()
for user_dic in users_dic:
if username == user_dic['name'] and passwd == user_dic['passwd']:
current_dic["username"] = username
current_dic["Login"] = True
res = func(*args, **kwargs)
return res
else:
print("用户名或者密码错误")
return wrapper
@auth_func #home = auth_func(home)
def home():
print("欢迎回家")
@auth_func #home = auth_func(home)
def shopping():
print("购物车里有:牛奶,面包,大米")
home()
shopping()
输出结果:
用户名:zsz
密码:123
欢迎回家
购物车里有:牛奶,面包,大米