鱼C论坛

 找回密码
 立即注册
查看: 1976|回复: 5

[已解决]第026讲:字典 课后测试题编写一个用户登录程序

[复制链接]
发表于 2019-12-29 16:50:19 | 显示全部楼层 |阅读模式

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

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

x
在老用户登录账号时如果设置密码连续三次输入错误将被锁定,我的这个for循环语句是不是有问题?就是如果用户输入的用户名密码匹配,成功登录OA系统也会报错”用户锁定,请联系系统管理员!“break应该如何设置比较好?
  1. dict1={}

  2. def new_user():
  3.    
  4.     user=input('请输入用户名:')
  5.     while user in dict1:
  6.         user=input('此用户名已经被使用,请重新输入:')
  7.     password=input('请输入密码:')
  8.     dict1[user]=password
  9.     print('用户注册成功!')
  10.    
  11. def old_user():
  12.     user=input('请输入用户名:')
  13.     if user in dict1:
  14.         for i in range(3):
  15.             password=input('请输入密码:')
  16.             if password==dict1[user]:
  17.                 print('欢迎登录OA系统!')
  18.                 break
  19.             else:
  20.                 print('密码有误,请核对,连续输错3次密码将被锁定!')
  21.         print('用户锁定,请联系系统管理员!')
  22.     else:
  23.         print('用户名不存在!')
  24.    

  25. def display_list():
  26.    
  27.     while True:
  28.         choice=input('''
  29.        |---新建用户:N/n---|
  30.        |---登录账号:E/e---|
  31.        |---退出程序:Q/q---|
  32.        |请输入指令代码:''')
  33.         if choice in 'NnEeQq':
  34.             if choice=='N' or choice=='n':
  35.                 new_user()
  36.             if choice=='E' or choice=='e':
  37.                 old_user()
  38.             if choice=='Q' or choice=='q':
  39.                 confirm=input('您确认要关闭程序吗?(Y/N)')
  40.                 if confirm=='Y':
  41.                     break
  42.                 else:
  43.                     continue
  44.         while choice not in 'NnEeQq':
  45.             print('您输入的指令代码错误,请重新输入!')
  46.             break

  47. display_list()
  48.                
复制代码
最佳答案
2019-12-29 18:26:50
zzong2019 发表于 2019-12-29 18:25
看到了您对我编写的代码做了优化,这个很好,是我没想到的。但是您有没有试验过,对于验证身份通过的用户 ...

是的,疏忽了。这样可以吗?

  1. dict1 = {}
  2. users = {}


  3. def new_user():
  4.     user = input('请输入用户名:')
  5.     while user in dict1:
  6.         user = input('此用户名已经被使用,请重新输入:')
  7.     password = input('请输入密码:')
  8.     dict1[user] = password
  9.     users[user] = False
  10.     print('用户注册成功!')


  11. def old_user():
  12.     user = input('请输入用户名:')
  13.     if user in dict1:
  14.         if users[user]:
  15.             print("用户锁定,请联系系统管理员!")
  16.             return
  17.         for i in range(3):
  18.             password = input('请输入密码:')
  19.             if password == dict1[user]:
  20.                 print('欢迎登录OA系统!')
  21.                 break
  22.             else:
  23.                 print('密码有误,请核对,连续输错 3 次密码将被锁定!')
  24.         else:
  25.             print('用户锁定,请联系系统管理员!')
  26.             users[user] = True
  27.     else:
  28.         print('用户名不存在!')


  29. def display_list():
  30.     while True:
  31.         choice = input('''|---新建用户:N/n---|
  32. |---登录账号:E/e---|
  33. |---退出程序:Q/q---|
  34. 请输入指令代码:''')
  35.         if choice in 'NnEeQq':
  36.             if choice == 'N' or choice == 'n':
  37.                 new_user()
  38.             if choice == 'E' or choice == 'e':
  39.                 old_user()
  40.             if choice == 'Q' or choice == 'q':
  41.                 confirm = input('您确认要关闭程序吗?(Y/N)')
  42.                 if confirm == 'Y':
  43.                     break
  44.                 else:
  45.                     continue
  46.         while choice not in 'NnEeQq':
  47.             print('您输入的指令代码错误,请重新输入!')
  48.             break


  49. display_list()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-12-29 17:21:32 | 显示全部楼层
  1. dict1 = {}
  2. users = {}


  3. def new_user():
  4.     user = input('请输入用户名:')
  5.     while user in dict1:
  6.         user = input('此用户名已经被使用,请重新输入:')
  7.     password = input('请输入密码:')
  8.     dict1[user] = password
  9.     users[user] = False
  10.     print('用户注册成功!')


  11. def old_user():
  12.     user = input('请输入用户名:')
  13.     if user in dict1:
  14.         if users[user]:
  15.             print("用户锁定,请联系系统管理员!")
  16.             return
  17.         for i in range(3):
  18.             password = input('请输入密码:')
  19.             if password == dict1[user]:
  20.                 print('欢迎登录OA系统!')
  21.                 break
  22.             else:
  23.                 print('密码有误,请核对,连续输错 3 次密码将被锁定!')
  24.         print('用户锁定,请联系系统管理员!')
  25.         users[user] = True
  26.     else:
  27.         print('用户名不存在!')


  28. def display_list():
  29.     while True:
  30.         choice = input('''|---新建用户:N/n---|
  31. |---登录账号:E/e---|
  32. |---退出程序:Q/q---|
  33. 请输入指令代码:''')
  34.         if choice in 'NnEeQq':
  35.             if choice == 'N' or choice == 'n':
  36.                 new_user()
  37.             if choice == 'E' or choice == 'e':
  38.                 old_user()
  39.             if choice == 'Q' or choice == 'q':
  40.                 confirm = input('您确认要关闭程序吗?(Y/N)')
  41.                 if confirm == 'Y':
  42.                     break
  43.                 else:
  44.                     continue
  45.         while choice not in 'NnEeQq':
  46.             print('您输入的指令代码错误,请重新输入!')
  47.             break


  48. display_list()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-29 18:25:25 | 显示全部楼层

看到了您对我编写的代码做了优化,这个很好,是我没想到的。但是您有没有试验过,对于验证身份通过的用户,下次再次登录该程序的时候就会提示用户被锁定?因为执行break后还是会跳到 print('用户锁定,请联系系统管理员!');users[user] = True这条语句上啊!
  1. for i in range(3):
  2.             password = input('请输入密码:')
  3.             if password == dict1[user]:
  4.                 print('欢迎登录OA系统!')
  5.                 break
  6.             else:
  7.                 print('密码有误,请核对,连续输错 3 次密码将被锁定!')
  8.         print('用户锁定,请联系系统管理员!')
  9.         users[user] = True
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-29 18:26:50 | 显示全部楼层    本楼为最佳答案   
zzong2019 发表于 2019-12-29 18:25
看到了您对我编写的代码做了优化,这个很好,是我没想到的。但是您有没有试验过,对于验证身份通过的用户 ...

是的,疏忽了。这样可以吗?

  1. dict1 = {}
  2. users = {}


  3. def new_user():
  4.     user = input('请输入用户名:')
  5.     while user in dict1:
  6.         user = input('此用户名已经被使用,请重新输入:')
  7.     password = input('请输入密码:')
  8.     dict1[user] = password
  9.     users[user] = False
  10.     print('用户注册成功!')


  11. def old_user():
  12.     user = input('请输入用户名:')
  13.     if user in dict1:
  14.         if users[user]:
  15.             print("用户锁定,请联系系统管理员!")
  16.             return
  17.         for i in range(3):
  18.             password = input('请输入密码:')
  19.             if password == dict1[user]:
  20.                 print('欢迎登录OA系统!')
  21.                 break
  22.             else:
  23.                 print('密码有误,请核对,连续输错 3 次密码将被锁定!')
  24.         else:
  25.             print('用户锁定,请联系系统管理员!')
  26.             users[user] = True
  27.     else:
  28.         print('用户名不存在!')


  29. def display_list():
  30.     while True:
  31.         choice = input('''|---新建用户:N/n---|
  32. |---登录账号:E/e---|
  33. |---退出程序:Q/q---|
  34. 请输入指令代码:''')
  35.         if choice in 'NnEeQq':
  36.             if choice == 'N' or choice == 'n':
  37.                 new_user()
  38.             if choice == 'E' or choice == 'e':
  39.                 old_user()
  40.             if choice == 'Q' or choice == 'q':
  41.                 confirm = input('您确认要关闭程序吗?(Y/N)')
  42.                 if confirm == 'Y':
  43.                     break
  44.                 else:
  45.                     continue
  46.         while choice not in 'NnEeQq':
  47.             print('您输入的指令代码错误,请重新输入!')
  48.             break


  49. display_list()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-29 18:46:18 | 显示全部楼层
zltzlt 发表于 2019-12-29 18:26
是的,疏忽了。这样可以吗?

成功了,感觉你这个else用的很巧妙,但是您能解读一下嘛?就是for循环三次之后结束的,就证明这个用户三次输错密码,然后还能回到这里嘛?
                             if users[user]:
                                 print("用户锁定,请联系系统管理员!")
                                 return
                             else:
                                  print('用户锁定,请联系系统管理员!')
                                  users[user] = True
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-25 15:11:33 | 显示全部楼层
你好,我是一个初学者,这个程序在正确输入用户名密码之后,怎么让它关闭呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-27 09:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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