userlist = [ {‘name‘:‘doudou‘,‘passwd‘:‘123‘}, {‘name‘:‘chouchou‘,‘passwd‘:‘456‘}, {‘name‘:‘liuhaiquan‘,‘passwd‘:‘789‘}, {‘name‘:‘liuxue‘,‘passwd‘:‘2020‘}] current_list = {‘username‘:None,‘login‘:False} #定义一个空的全局变量 def jingdong(yanzheng): def yonghu(*args,**kwargs): if current_list[‘username‘] and current_list[‘login‘]: #判断是否已经有用户输入 res = yanzheng(*args,**kwargs) #运行装饰器 return res username = input(‘用户名: ‘).strip() passwd = input(‘密码: ‘).strip() for user_dic in userlist: if username == user_dic[‘name‘] and passwd == user_dic[‘passwd‘]: current_list[‘username‘] = username current_list[‘login‘] = True res = yanzheng(*args,**kwargs) #运行装饰器 return res else: #如果for循环结束为止,还是没有验证成功,则打印密码或用户错误 print(‘用户或密码错误‘) return yonghu @jingdongdef desk(name): print(‘欢迎来到京东 %s‘ %name) @jingdongdef shopcar(name): print(‘%s 的购物车里有[%s,%s,%s]‘ %(name,‘水果‘,‘饮料‘,‘饼干‘)) @jingdongdef mina(name): print(‘显示%s的主页‘ %name) desk(‘chouchou‘)shopcar(‘dd‘)mina(‘kl‘)
原文地址:https://www.cnblogs.com/lhqlhq/p/8746682.html
时间: 2024-10-15 04:59:59