装饰器实战.py 958 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: UTF-8 -*-
  2. import sys
  3. user_info = {'name':'xdc', 'password':'123'}
  4. def auth_login(func):
  5. def wrapper(*args, **kwargs):
  6. inp_user = input('用户名:').strip()
  7. inp_pwd = input('密码:').strip()
  8. user = False
  9. if inp_user == user_info['name'] and inp_pwd == user_info['password']:
  10. user = user_info['name']
  11. if user:
  12. func(*args, **kwargs)
  13. else:
  14. print("用户名或密码错误")
  15. return wrapper
  16. def index():
  17. print('炫酷的主页')
  18. @auth_login
  19. def user_center():
  20. print('用户中心')
  21. def main():
  22. while 1:
  23. print("""
  24. 1、访问首页
  25. 2、访问用户中心
  26. 3、退出
  27. """)
  28. select = input('>>:')
  29. if select == '1':
  30. index()
  31. elif select == '2':
  32. user_center()
  33. elif select == '3':
  34. sys.exit()
  35. if __name__ == "__main__":
  36. main()