python程序—用戶登陸

編寫一個用戶登陸程序:spa

1.登陸成功顯示登陸頁面code

2.登陸失敗,顯示密碼錯誤,而且顯示錯誤幾回blog

3.登陸失敗三次,退出程序ip

username= 'root' passwd= '123' count=0 print('請登陸 >>>>>>>>>') while True: user=input('登陸名:') pwd=input('密碼:') if user == username and pwd == passwd: print('登陸成功!歡迎登陸!') break
    else: count +=1 print('密碼錯誤!登陸失敗!',count) if count ==3: break

升級:utf-8

支持多用戶登陸input

userinfo={ 'root': {'username': 'root', 'passwd': '123'}, 'lee' : {'username': 'lee', 'passwd': '10086'}, 'zhang':{'username': 'zhang', 'passwd': '10010'} } count=0 print('請登陸 >>>>>>>>>') while True: user=input('登陸名:').strip() pwd=input('密碼:').strip() if user == userinfo[user]['username'] and pwd == userinfo[user]['passwd']: print('登陸成功!歡迎登陸!') break
    else: count +=1 print('密碼錯誤!登陸失敗!') if count ==3: break

再次升級:it

1.支持建立用戶,並將用戶信息寫入文件class

2.同一用戶因密碼錯誤而登陸失敗三次後,提示用戶鎖定import

import re # 導入re模塊,進行正則匹配 userinfo1 = {} g = open('C:\\Users\\lenovo\\Desktop\\b.txt', 'r', encoding='utf-8') for i in g: user = re.compile('(.*?) (.*?) ').search(i).group(1) pwd = re.compile('(.*?) (.*?) ').search(i).group(2) count = re.compile('(.*?) (.*?) (.*)').search(i).group(3) userinfo1[user] = {'username': user, 'passwd': pwd, 'count': int(count)} g.close() while True: print('=======================================') print(' 1.建立用戶 2.登陸用戶 3.退出 ') print('=======================================') choose=input('請輸入選項:') if choose == '1': userinfo = {} with open('C:\\Users\\lenovo\\Desktop\\b.txt', 'w', encoding='utf-8') as f: print('請建立 >>>>>>>>>') userinfo1 = {} for i in open('C:\\Users\\lenovo\\Desktop\\b.txt', 'r', encoding='utf-8'): user = re.compile('(.*?) (.*?) ').search(i).group(1) pwd = re.compile('(.*?) (.*?) ').search(i).group(2) userinfo1[user] = {'username': user, 'passwd': pwd, 'count': 0} user = input('請輸入用戶名:').strip() if user in userinfo1.keys(): print('用戶名已經存在!') else: pwd = input('請輸入密碼:').strip() userinfo[user] = {'username': user, 'passwd': pwd, 'count': 0} for i in userinfo.values(): j=0
                for j in i.values(): f.write('%s ' % str(j)) f.write('\n') elif choose == '2': print('請登陸 >>>>>>>>>') with open('C:\\Users\\lenovo\\Desktop\\a.txt','r',encoding='utf-8') as f: user = input('登陸名:').strip() if user not in userinfo1: print('用戶不存在!') continue
            if user in f.read(): print(f.read()) print('用戶已鎖定!請聯繫管理員!') continue
            if user == userinfo1[user]['username']: pwd = input('密碼:').strip() if pwd == userinfo1[user]['passwd']: print('登陸成功!歡迎登陸!') continue
                else: userinfo1[user]['count']+=1 print('密碼錯誤!登陸失敗!') if userinfo1[user]['count']==3: with open('C:\\Users\\lenovo\\Desktop\\a.txt','a',encoding='utf-8') as f: f.write('%s \n' % user) elif choose == '3': break

    else: print('請輸入正確選項!')
相關文章
相關標籤/搜索