模拟登陆:
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户
思路:
1. 用户名密码文件为passwd,锁定用户文件为lock
2. 用户输入账号密码采用input输入,分割passwd文件出user,passwd字段并比较input的user和passwd
3.当用户三次输入错误后将input user写入到lock文件,读取时判断是否在lock文件中存在
流程图:
代码展示(Python 3.6):
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
"""模拟登陆作业"""
import time,sys,getpass
print("Welcome Please input your username and shopping_db ")
account_file = 'passwd'
lock_file = 'lock'
count = 0 while count <3:
username = input("username : ")
password = input("Passwd : ")
#password = None
#password = getpass.getpass("Passwd : ") #可以使用密文或者明文
lock_f = open("lock", "r+")
lock_userlist = lock_f.readlines() for lock_user in lock_userlist:
lock_user = lock_user.strip('\n')
if username == lock_user:
print("%s is lockd 30s later this program will end " % lock_user)
time.sleep(30)
sys.exit() with open("passwd", "r") as f:
userlist = f.readlines()
for user_line in userlist:
(user, passwd) = user_line.strip('\n').split()
if username == user:
if password == passwd:
print('welcome %s ' % username)
sys.exit(0)
elif password == None:
print("not allow none")
else:
pass
else:
print("sorry username or shopping_db wrong try again" )
count += 1 else:
lock_f.write(username + '\n')
sys.exit("please restart and user:%s is locked " %username)
lock_f.close()
passwd文件内容
colin 123456
python 123456
golang 123456
lock文件内容
colin1
colin2