鱼C论坛

 找回密码
 立即注册
查看: 1482|回复: 0

[见证历程] Python 登录/注册 简单封装函数

[复制链接]
发表于 2019-3-14 17:34:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. # 定义指令主菜单
  2. MenuNote = '''
  3. =====================欢迎使用用户登录/注册小程序=================

  4.                         |--- 新建用户:N/n ---|
  5.                         |--- 登录账号:E/e ---|
  6.                         |--- 退出程序:Q/q ---|

  7. '''
  8. # 存放用户名密码的字典
  9. userInfo = {}
  10. # 判断用户名唯一
  11. def userNameIsUnique(userName):
  12.     if userName in userInfo:
  13.         return False
  14.     else:
  15.         return True
  16. # 判断密码与确认密码是否一致
  17. def userPassCheck(userPass,userPassSecond):
  18.     if userPass == userPassSecond:
  19.         print("密码设置成功")
  20.         print(MenuNote)
  21.         return True
  22.     else:
  23.         print("两次密码输入不一致,请重新设置")
  24.         return False
  25. #用户密码匹配校验
  26. def passwordMatching(userName,userPass):
  27.     if userInfo[userName] == userPass:
  28.         print("密码验证通过,登录成功")
  29.         return True
  30.     else:
  31.         print("用户名与密码不匹配")
  32.         return False
  33. #空字符串或空字符校验
  34. def isEmpty(dataString):
  35.     flag = 1 if  dataString.strip() is '' else 0
  36.     return  flag
  37. #设置密码
  38. def setUserPass(userName):
  39.     while True:
  40.         userpassFirst = input("请为" + userName + "设置密码(第1次)")
  41.         if isEmpty(userpassFirst):
  42.             print("密码不能为空,请重设")
  43.             continue
  44.         userPassSecond = input("请为" + userName + "设置密码(第2次)")
  45.         isSamePassFlag = userPassCheck(userpassFirst, userPassSecond)
  46.         if isSamePassFlag:
  47.             userInfo[userName] = userpassFirst
  48.             print(userInfo)
  49.             break

  50. # 新建用户模块
  51. def newUser(userName1):
  52.     while True :
  53.         userName = input("请输入注册用户名: ") if isEmpty(userName1)  else userName1
  54.         while True :
  55.             if isEmpty(userName):
  56.                 userName = input("用户名不能为空,请重新输入")
  57.             else:
  58.                 break
  59.         isUniqueFlag = userNameIsUnique(userName)
  60.         #用户名是否唯一
  61.         if isUniqueFlag:
  62.             while True:
  63.                 userpassFirst = input("请为"+userName+"设置密码(第1次):")
  64.                 if isEmpty(userpassFirst):
  65.                     print("密码不能为空,请重设")
  66.                     continue
  67.                 userPassSecond = input("请为"+userName+"设置密码(第2次):")
  68.                 isSamePassFlag = userPassCheck(userpassFirst,userPassSecond)
  69.                 if isSamePassFlag :
  70.                     userInfo[userName] = userpassFirst
  71.                     print(userInfo)
  72.                     break


  73.             break
  74.         else:
  75.             print("用户名重复")
  76. # 登录模块
  77. def userLogin():
  78.     while True:
  79.         userName = input("请输入登录账号: ")
  80.         if isEmpty(userName) == 0:
  81.             if userNameIsUnique(userName) == False:
  82.                 userPass = input("请输入密码: ")
  83.                 isMatch =  passwordMatching(userName, userPass)
  84.                 if isMatch == True:
  85.                     print(MenuNote)
  86.                 else:
  87.                     resetPasswordCode = input('是否重设密码?(Y/N)')
  88.                     while True:
  89.                         if resetPasswordCode == 'Y' or resetPasswordCode == 'y':
  90.                             setUserPass(userName)
  91.                             break
  92.                         elif resetPasswordCode == 'N' or resetPasswordCode == 'n':
  93.                             print('您选择了不重设密码,将回到主页面')
  94.                             print(MenuNote)
  95.                             break
  96.                         else:
  97.                             resetPasswordCode = input('指令不存在,请重新选择是否重设密码?(Y/N)')
  98.             elif userNameIsUnique(userName) == True:
  99.                 isNewUserCode = input("用户不存在,是否新建?(Y/N)")
  100.                 while True:
  101.                     if isNewUserCode == 'Y' or isNewUserCode == 'y':
  102.                         newUser(userName)
  103.                         break
  104.                     elif isNewUserCode == 'N' or isNewUserCode == 'n':
  105.                         print("你选择了否,将退回主菜单")
  106.                         print(MenuNote)
  107.                         break
  108.                     else:
  109.                         isNewUserCode = input('你输入的指令错误,请重新输入是否新建(Y:是 N:否)')
  110.             break
  111.         else:
  112.             print("用户不能为空")

  113. # 主函数部分
  114. print(MenuNote)
  115. username = ''
  116. while True:
  117.     code = input("请输入指令代码: ")
  118.     if(code == 'N' or code == 'n'):
  119.         newUser(username)
  120.     elif(code == 'E' or code == 'e'):
  121.         userLogin()
  122.     elif(code == 'Q' or code == 'q'):
  123.         print("感谢使用,再见!")
  124.         break
  125.     else:
  126.         print("指令不存在,请重新输入")

复制代码


业务规则:
①新建用户,用户登录,退出的功能
②用户名,密码不能为空
③登录时用户不存在可直接新建
④用户密码匹配校验
⑤指令不存在的提示
⑥设置密码输2遍

----------------------测试场景--------
序号        场景覆盖测试
1        新建用户,输入唯一用户名,设置2次相同的密码,
2        新建用户,输入唯一用户名,设置2次不相同的密码,
3        新建用户,输入已存在的用户名
4        用户登录,输入不存在的用户名,是否新建选择‘是’,设置2次相同密码
5        用户登录,输入不存在的用户名,是否新建选择‘是’,设置2次不相同密码
6        用户登录,输入不唯一用户名,是否新建选择‘否’
7        用户登录,输入不唯一用户名,是否新建输入不存在的指令
8        用户登录,输入已存在的用户名,密码输入正确
9        用户登录,输入已存在的用户名,密码输入不正确
10        输入不存在的菜单指令
11        退出
12        新建用户,设置密码为空
13        新建用户,设置用户名为空
14        登录,输入密码不对,重设置密码选择‘是’
15        登录,输入密码不对,重设置密码选择‘否’
16        登录,重设用户名为空
17        登录,重设密码为空

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 20:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表