zzong2019 发表于 2019-12-29 16:50:19

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

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

def new_user():
   
    user=input('请输入用户名:')
    while user in dict1:
      user=input('此用户名已经被使用,请重新输入:')
    password=input('请输入密码:')
    dict1=password
    print('用户注册成功!')
   
def old_user():
    user=input('请输入用户名:')
    if user in dict1:
      for i in range(3):
            password=input('请输入密码:')
            if password==dict1:
                print('欢迎登录OA系统!')
                break
            else:
                print('密码有误,请核对,连续输错3次密码将被锁定!')
      print('用户锁定,请联系系统管理员!')
    else:
      print('用户名不存在!')
   

def display_list():
   
    while True:
      choice=input('''
       |---新建用户:N/n---|
       |---登录账号:E/e---|
       |---退出程序:Q/q---|
       |请输入指令代码:''')
      if choice in 'NnEeQq':
            if choice=='N' or choice=='n':
                new_user()
            if choice=='E' or choice=='e':
                old_user()
            if choice=='Q' or choice=='q':
                confirm=input('您确认要关闭程序吗?(Y/N)')
                if confirm=='Y':
                  break
                else:
                  continue
      while choice not in 'NnEeQq':
            print('您输入的指令代码错误,请重新输入!')
            break

display_list()
               

zltzlt 发表于 2019-12-29 17:21:32

dict1 = {}
users = {}


def new_user():
    user = input('请输入用户名:')
    while user in dict1:
      user = input('此用户名已经被使用,请重新输入:')
    password = input('请输入密码:')
    dict1 = password
    users = False
    print('用户注册成功!')


def old_user():
    user = input('请输入用户名:')
    if user in dict1:
      if users:
            print("用户锁定,请联系系统管理员!")
            return
      for i in range(3):
            password = input('请输入密码:')
            if password == dict1:
                print('欢迎登录OA系统!')
                break
            else:
                print('密码有误,请核对,连续输错 3 次密码将被锁定!')
      print('用户锁定,请联系系统管理员!')
      users = True
    else:
      print('用户名不存在!')


def display_list():
    while True:
      choice = input('''|---新建用户:N/n---|
|---登录账号:E/e---|
|---退出程序:Q/q---|
请输入指令代码:''')
      if choice in 'NnEeQq':
            if choice == 'N' or choice == 'n':
                new_user()
            if choice == 'E' or choice == 'e':
                old_user()
            if choice == 'Q' or choice == 'q':
                confirm = input('您确认要关闭程序吗?(Y/N)')
                if confirm == 'Y':
                  break
                else:
                  continue
      while choice not in 'NnEeQq':
            print('您输入的指令代码错误,请重新输入!')
            break


display_list()

zzong2019 发表于 2019-12-29 18:25:25

zltzlt 发表于 2019-12-29 17:21


看到了您对我编写的代码做了优化,这个很好,是我没想到的。但是您有没有试验过,对于验证身份通过的用户,下次再次登录该程序的时候就会提示用户被锁定?因为执行break后还是会跳到 print('用户锁定,请联系系统管理员!');users = True这条语句上啊!
for i in range(3):
            password = input('请输入密码:')
            if password == dict1:
                print('欢迎登录OA系统!')
                break
            else:
                print('密码有误,请核对,连续输错 3 次密码将被锁定!')
      print('用户锁定,请联系系统管理员!')
      users = True

zltzlt 发表于 2019-12-29 18:26:50

zzong2019 发表于 2019-12-29 18:25
看到了您对我编写的代码做了优化,这个很好,是我没想到的。但是您有没有试验过,对于验证身份通过的用户 ...

是的,疏忽了。这样可以吗?{:10_254:}

dict1 = {}
users = {}


def new_user():
    user = input('请输入用户名:')
    while user in dict1:
      user = input('此用户名已经被使用,请重新输入:')
    password = input('请输入密码:')
    dict1 = password
    users = False
    print('用户注册成功!')


def old_user():
    user = input('请输入用户名:')
    if user in dict1:
      if users:
            print("用户锁定,请联系系统管理员!")
            return
      for i in range(3):
            password = input('请输入密码:')
            if password == dict1:
                print('欢迎登录OA系统!')
                break
            else:
                print('密码有误,请核对,连续输错 3 次密码将被锁定!')
      else:
            print('用户锁定,请联系系统管理员!')
            users = True
    else:
      print('用户名不存在!')


def display_list():
    while True:
      choice = input('''|---新建用户:N/n---|
|---登录账号:E/e---|
|---退出程序:Q/q---|
请输入指令代码:''')
      if choice in 'NnEeQq':
            if choice == 'N' or choice == 'n':
                new_user()
            if choice == 'E' or choice == 'e':
                old_user()
            if choice == 'Q' or choice == 'q':
                confirm = input('您确认要关闭程序吗?(Y/N)')
                if confirm == 'Y':
                  break
                else:
                  continue
      while choice not in 'NnEeQq':
            print('您输入的指令代码错误,请重新输入!')
            break


display_list()

zzong2019 发表于 2019-12-29 18:46:18

zltzlt 发表于 2019-12-29 18:26
是的,疏忽了。这样可以吗?

成功了,感觉你这个else用的很巧妙,但是您能解读一下嘛?就是for循环三次之后结束的,就证明这个用户三次输错密码,然后还能回到这里嘛?
                           if users:
                                 print("用户锁定,请联系系统管理员!")
                                 return
                           else:
                                  print('用户锁定,请联系系统管理员!')
                                  users = True

千霜碎岳 发表于 2021-2-25 15:11:33

你好,我是一个初学者,这个程序在正确输入用户名密码之后,怎么让它关闭呢
页: [1]
查看完整版本: 第026讲:字典 课后测试题编写一个用户登录程序