1. 程式人生 > >python--使用者登入

python--使用者登入

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:xuejp

import sys

#輸入使用者名稱密碼
username = input('username:')
password = input("password:")

#讀取檔案並轉化為字典
with open('./User','r') as f:
    result = dict(line.strip().split(':') for line in f if line)

#設定計數器
count = 0

#使用者是否存在
def if_user_exit():
    count = 0
for key in result: if username == key: count+=1 if count == 0: print('The user is not exist') sys.exit() #檢查使用者是否被鎖 def if_user_lock(): with open('./lock_user', 'r') as f: # 檢車該使用者是否被lock for line in f.readlines(): if username == line.strip(): print('The user is locked'
) sys.exit() if_user_exit() if_user_lock() while count < 3: if password == result.get(username): #使用者名稱密碼正確跳到登入介面 info = '''welcome user {_username} login'''.format(_username=username) print(info) break else: #使用者名稱密碼不正確,告知登入失敗,計數器加一 if_user_exit() #判斷第二次輸入的使用者是否存在
count += 1 if count == 3: # 首先判斷是否錯誤三次 print('sorry,the user %s is locked!' % (username)) # 第三次登入失敗告知使用者被鎖 with open('./lock_user', 'a') as f_lock: # 將使用者名稱寫到檔案中 f_lock.write(username + '\n') else: print("login filed:three filed will be lock,only have {_count} changes".format(_count=3-count)) username = input('username:') password = input("password:")

流程圖
這裡寫圖片描述