登陸流程圖:

代碼實現:
#-*- coding=utf-8 -*-import os,sys,getpass'''user.txt 格式賬號 密碼 是否鎖定 錯誤次數jack 123 unlock 0tom 123 unlock 0lily 123 unlock 0hanmeimei 123 unlock 0lucy 123 unlock 0'''# 定義寫入文件的函數def wirte_to_user_file(users,user_file_path): user_file = file(user_file_path,'w+') for k,v in users.items(): line = [] line.append(k) line.extend(v) user_file.write(' '.join(line)+'/n') user_file.close()# 判斷用戶文件是否存在,不存在直接退出user_file_path = 'users.txt'if os.path.exists(user_file_path): user_file = file(user_file_path,'r')else: print 'user file is not exists' sys.exit(1)# 遍歷用戶文件,將用戶包裝成字典users_dic = {}for user_line in user_file: user = user_line.strip().split() users_dic[user[0]] = user[1:]'''{ 'lucy': ['123', 'unlock', '0'], 'lily': ['123', 'unlock', '0'], 'jack': ['123', 'unlock', '0'], 'hanmeimei': ['123', 'unlock', '0'], 'tom': ['123', 'unlock', '0']}'''while True: # 輸入賬號 input_name = raw_input('please input your username,input "quit" or "q" will be exit : ').strip() # 判斷是否為退出 if input_name == 'quit' or input_name == 'q': sys.exit(0) # 輸入密碼 password = getpass.getpass('please input your password:').strip() # 判斷賬號是否存在、是否鎖定 if input_name not in users_dic: print 'username or password is not right' break if users_dic[input_name][1] == 'lock': print 'user has been locked' break # 判斷密碼是否正確,正確,登陸成功 if str(password) == users_dic[input_name][0]: print 'login success,welcome to study system' sys.exit(0) else: # 如果密碼錯誤則修改密碼錯誤次數 users_dic[input_name][2] = str(int(users_dic[input_name][2])+1) # 密碼錯誤次數大于3的時候則鎖定,并修改狀態 if int(users_dic[input_name][2]) >= 3: print 'password input wrong has 3 times,user will be locked,please connect administrator' users_dic[input_name][1] = 'lock' wirte_to_user_file(users_dic,user_file_path) break wirte_to_user_file(users_dic,user_file_path)以上這篇python實現簡單登陸流程的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答