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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

  



以上是关于python实现登录验证(循环练习)的主要内容,如果未能解决你的问题,请参考以下文章

Python学习心得第一周-03练习1

Python初学练习01:简易登录验证

Python学习:8.小项目之登录注册验证

Python(73)_装饰器函数练习_执行函数前登录验证

Python+Selenium练习(二十)-验证控件是否被选中

Python实现注册和三次验证登录