码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础-用户验证

时间:2017-04-08 11:13:01      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:mod   src   pen   count   三次   rip   view   for   adl   

一、项目需求

  1、根据用户名和密码,验证用户是否可登陆

  2、允许一次执行可验证三次

  3、当用户名输错三次后,该用户名锁定,永久不可登陆

 

二、代码如下

  

技术分享
 1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 import operator
 4 n = 0
 5 username="user123"
 6 password="123.abc"
 7 count_dict={}
 8 with open("lock",mode="r+",encoding="utf8") as f_read,open("lock_tmp",mode="r+",encoding="utf8") as f_write:
 9     while n<3:
10         name=input("please input username:").strip()
11         pwd=input("please input passwd:").strip()
12         for line in f_read:
13             if name in line:
14                 print("当前用户已经被锁定,请联系管理员!!")
15                 exit()
16         if name == username and pwd == password:
17             print("欢迎%s登录"%username)
18             exit()
19         else:
20             print("用户名或密码错误,请重新输入!")
21             f_write.write(name+"\n")
22         n+=1
23 
24     for line in f_write.readlines():
25         line = line.strip()
26         count = count_dict.setdefault(line,0)
27         count+=1
28         count_dict[line]=count
29     for k in count_dict.keys():
30         print(k)
31         print(count_dict[k])
32         if count_dict[k] >= 3:
33             f_read.write(k+"\n")
View Code

 

Python基础-用户验证

标签:mod   src   pen   count   三次   rip   view   for   adl   

原文地址:http://www.cnblogs.com/baomanji/p/6680996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!