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:            passelse:    print("%s is not register" %(username))


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章