模拟登陆:
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户
思路:
1. 用户名密码文件为passwd,锁定用户文件为lock
2. 用户输入账号密码采用input输入,分割passwd文件出user,passwd字段并比较input的user和passwd
3.当用户三次输入错误后将input user写入到lock文件,读取时判断是否在lock文件中存在
流程图:
代码展示(Python 3.6):
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author: Colin Yao 4 """模拟登陆作业""" 5 import time,sys,getpass 6 print("Welcome Please input your username and shopping_db ") 7 account_file = 'passwd' 8 lock_file = 'lock' 9 count = 0 10 11 while count <3: 12 username = input("username : ") 13 password = input("Passwd : ") 14 #password = None 15 #password = getpass.getpass("Passwd : ") #可以使用密文或者明文 16 lock_f = open("lock", "r+") 17 lock_userlist = lock_f.readlines() 18 19 for lock_user in lock_userlist: 20 lock_user = lock_user.strip('\n') 21 if username == lock_user: 22 print("%s is lockd 30s later this program will end " % lock_user) 23 time.sleep(30) 24 sys.exit() 25 26 with open("passwd", "r") as f: 27 userlist = f.readlines() 28 for user_line in userlist: 29 (user, passwd) = user_line.strip('\n').split() 30 if username == user: 31 if password == passwd: 32 print('welcome %s ' % username) 33 sys.exit(0) 34 elif password == None: 35 print("not allow none") 36 else: 37 pass 38 else: 39 print("sorry username or shopping_db wrong try again" ) 40 count += 1 41 42 else: 43 lock_f.write(username + '\n') 44 sys.exit("please restart and user:%s is locked " %username) 45 lock_f.close()
passwd文件内容
colin 123456 python 123456 golang 123456
lock文件内容
colin1
colin2