python程序—用戶登錄


編寫一個用戶登錄程序:

1.登錄成功顯示登錄頁面

2.登錄失敗,顯示密碼錯誤,並且顯示錯誤幾次

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

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

升級:

支持多用戶登錄

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

再次升級:

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

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

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('請輸入正確選項!')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM