python之用户登陆作业

实现一个用户登陆的小程序,如果密码错误,可以重试三次,如果三次均错误,则锁定

数据库如下格式,分别用户名|密码|锁定,其中锁定字段0为正常,1为被锁定

root|admin123|0
admin|admin123|1
Cevin|admin123|0

代码如下

username = input("please input your username:")
file_db = open('db','r',encoding='utf-8')
file = open('db','r',encoding='utf-8')
test_list = []
for line in file:
    f = line.strip().split('|')
    test_list.append(f[0])
file.close()
if username in test_list:
    for line in file_db:
        list_db = line.strip().split('|')
        if list_db[0] == username and list_db[2] == '0':
            password = input("please input your password:")
            if list_db[1] == password:
                print('welcome %s login.' %(username))
                break
            else:
                print("password is error")
                for i in range(4):
                    if i == 3:
                        print("%s is already locked" % (username))
                        file_db = open('db','r',encoding='utf-8')
                        file_new_db = open('new-db','w',encoding='utf-8')
                        for line in file_db:
                            if username in line:
                                test_list = line.strip().split('|')
                                file_new_db.write(test_list[0]+ '|' + test_list[1] +'|' + '1' + '\n')
                            else:
                                file_new_db.write(line)
                        file_new_db.close()
                        file_db.close()
                        break
                    else:
                        option = input("do you want do to tyr?")
                        if option == 'no':
                            print("ByeBye")
                            break
                        else:
                            password = input("please input your password:")
                            if list_db[1] == password:
                                print("yes,you have already login success!")
                                break
                            else:
                                print("password is error")
        elif list_db[0] == username and list_db[2] == '1':
              print("%s is already locked" %(username))
              break
        else:
            pass
else:
    print("%s is not register" %(username))

 

转载于:https://www.cnblogs.com/bainianminguo/p/6715137.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值