模拟登陆:
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "macel"
# Date: 2017/4/27
with open('userdata.txt', 'r') as data,open('lock.txt', 'r') as locked:
flag = False
List= []
while flag == False :
username = input('please input your username:')
#将光标放到data的开始位置
data.seek(0)
for line in data:
if line.strip().split(':')[0] == username:
flag = True
count = 0
while flag:
locked.seek(0)
for line in locked:
if line.strip() == username:
print('sorry,your accout has been locked!')
exit()
data.seek(0)
password = input('please input your password:')
for line in data:
name = line.strip().split(":")[0]
passwd = line.strip().split(":")[1]
List.append(username)
if name==username and passwd==password :
print('Welcome!')
flag= False
exit()
elif name==username and passwd!=password :
count += 1
if count==3:
with open('lock.txt', 'a') as locked:
locked.write(username)
locked.write('\n')
print('your account has been locked!')
flag = False
break
else:
print('wrong password,please try again!')