Python之路 day1 用户登录多次被锁定
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python之路 day1 用户登录多次被锁定相关的知识,希望对你有一定的参考价值。
编写登陆接口:
- 输入用户名密码
- 认证成功后显示欢迎信息
- 输错三次后锁定
1 #Author:ersa 2 import getpass,os,sys 3 4 #读取账户信息到内存中 5 try: 6 accounts_file = open("E:/LinuxStudy32/Python/accounts.txt", "r") 7 except IOError: 8 print("The file don‘t exitst,Please double check!") 9 exit() 10 11 accounts_info = accounts_file.readlines() 12 accounts_file.close() 13 14 #write username to lock accounts file 15 try: 16 lock_file = open("E:/LinuxStudy32/Python/lock_accounts.txt","r") 17 except IOError: 18 print("The file don‘t exitst,Please double check!") 19 exit() 20 lock_users = lock_file.readlines() 21 lock_file.close() 22 23 #passwd = getpass.getpass("passwd:") 24 25 count = 0 26 27 while count<3: 28 username = input("username:") 29 passwd = input("passwd:") 30 end_loop = 0 31 for user in lock_users: 32 user = user.rstrip() #除掉行末尾的空格、回车符 33 if user == username: 34 exit("Invalid {name},your account is locked!".format(name=username)) 35 36 account = username + " " + passwd 37 for userinfo in accounts_info: 38 userinfo = userinfo.rstrip() #除掉行末尾的空格、回车符 39 if userinfo == account: 40 exit("Welcome user {name} login...".format(name=username)) 41 42 count += 1 43 if count == 3: 44 try: 45 lock_file = open("E:/LinuxStudy32/Python/lock_accounts.txt", "a") 46 except IOError: 47 print("The file don‘t exitst,Please double check!") 48 exit() 49 username += "\n" 50 lock_file.write(username) 51 lock_file.close() 52 print("Invalid {name},your account is lock...".format(name=username)) 53 else: 54 print("you have tried too many times...fuck off") 55 56 #print(username,passwd)
以上是关于Python之路 day1 用户登录多次被锁定的主要内容,如果未能解决你的问题,请参考以下文章