python实现登录验证(循环练习)

练习python的while循环控制,模拟登录验证,登录失败三次会锁定账户。
login_validate.py
##!/usr/bin/env python
# -*- coding: UTF-8 -*-
#author:lonerangerr
count = 0
retry_limit = 3
while count < retry_limit:
    username = raw_input("Enter your username:")
    with open('lockfile','r') as f:
        for i in f.readlines():
            if username == i.split()[0]:
                print "Sorry,%s:your account is locked,process will be quit..."%username
         f.close() exit() password
= raw_input("Enter your passwd:") match = False with open('userinfo','r') as f: for line in f.readlines(): user,passwd = line.strip('\n').split() if username == user and password == passwd: #print "yyyyyyyyyyyyyyyyyyes..." pass match = True break if match ==False: print "Unmatched!!! u still have %s times to retry..."%(2-count) count += 1 else: print "Hello,%s: Welcome to login..."%username break f.close() else: print "Sorry,%s:your account will be locked..."%username with open('lockfile','a') as f: f.write(username) f.write('\n') f.close()

运行此程序需要的另外两个文件(三个文件放在同一个目录下)lockfile(被锁定用户)、userinfo(用户账户、密码信息),格式如下

lockfile

locked_user1
locked_user2
locked_user3

userinfo

user1    111
user2    111
user3    111

  

转载于:https://www.cnblogs.com/lonerangerr/p/6684584.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值