登陸接口練習python
要求:讀取賬號文件,登陸三次錯誤密碼將鎖定並寫入鎖定文件ide
# python 3.6 登陸接口練習 # _*_ coding:utf-8 _*_ import sys import os import getpass i=0 while i < 3 : username = input("請輸入用戶名:") with open("account_lock.txt","r") as account_lock_file : #打開鎖定用戶文件,r爲只讀 account_lock_list = account_lock_file.readlines() #轉換成列表 for lock_name in account_lock_list : #循環遍歷文件 lock_name = lock_name.strip('\n') #去掉換行符 if username == lock_name : sys.exit("用戶{_username}已被鎖定,請聯繫管理員解鎖!!".format(_username=username)) #password =input("請輸入密碼:") with open("accounts.txt",'r') as account_file : account_list = account_file.readlines() for account_line in account_list : (name,pas) = account_line.strip('\n').split() #去掉換行符,並以,爲切片 if username == name : j = 0 while j < 3 : password = getpass.getpass("請輸入用戶密碼:") if password == pas : print("用戶{_name}登陸成功".format(_name = username)) sys.exit() else: if j != 2 : print("用戶{_name}密碼輸入錯誤,請從新輸入,還有{d}次機會".format(_name = username,d=2-j)) j = j + 1 else: with open("account_lock.txt",'a+') as account_lock_file : account_lock_file.write(username +'\n') sys.exit("超過最大錯誤次數,用戶{_name}已被鎖定,並退出".format(_name=username)) else: pass else: if i != 2 : print("用戶{_name}不存在,請從新輸入,還有{d}次機會!".format(_name = username,d=2-i)) i = i + 1 #當用戶輸入用戶名錯誤時增長錯誤次數 else: sys.exit("用戶{_username}不存在,系統即將退出".format(_username=username))