首页 > 代码库 > python实现登录验证(循环练习)
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实现登录验证(循环练习)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。