简单的登录接口

项目名称:简单的登录接口

项目要求:

1.输入用户名和用户密码

2.输入成功后会显示欢迎信息

3.三次输入失败后就会锁定用户

项目说明:

输入的用户名都是user.txt或者lock.txt文件中的用户名,如果输入的用户名不存在于这两个文件中就会提示输入的用户名不正确警告。

项目流程图:

项目代码:

 1 #encoding=utf-8
 2 __author__ = 'heng'
 3 """
 4 ---------------------------------------------------------------------------------------------------------------------------
 5 项目名称:简单的登录接口
 6 项目要求:
 7 1.输入用户名和用户密码
 8 2.输入成功后会显示欢迎信息
 9 3.三次输入失败后就会锁定用户
10 项目说明:
11 输入的用户名都是user.txt或者lock.txt文件中的用户名,如果输入的用户名不存在于这两个文件中就会提示输入的用户名不正确警告。
12 ---------------------------------------------------------------------------------------------------------------------------
13 """
14 import sys
15 NOTmatch = True    #用来判断用户是否存在
16 user = file(r'F:python projectsimpleLogAPIuser.txt')
17 lock_user = file(r'F:python projectsimpleLogAPIlock.txt')
18 while True:
19     input_user = raw_input('plese enter the user:')
20     for lock in lock_user.readlines():             #遍历锁定用户文档中的所有用户
21         lock = lock.split()
22         if input_user == lock[0]:                     #判断每一行的列表中是否含有用户名
23             NOTmatch = False
24             print "the user has been locked!"
25             lock_user.close()
26     else:
27         for line1 in user.readlines():
28             count = 0      #记录输入密码的错误
29             line1 = line1.split()
30             if input_user in line1:
31                 NOTmatch = False
32                 while count < 3:
33                     input_password = raw_input('please enter the password:')
34                     if input_password == line1[1]:
35                         print 'welcome you !'
36                         user.close()
37                         sys.exit()
38                     else:
39                         count +=1
40                         print' your password is error,you have three chances try.input more three times the user was locked! '
41                 else:                #如果输入错误次数超过3次
42                     lock_user = file(r'F:python projectsimpleLogAPIlock.txt','ab')
43                     lock_user.write(input_user)
44                     print 'you have three input error! the user was  locked!'
45 
46     if NOTmatch:             #判断输入的用户名是否存在
47         print 'your user is not in the basedata'
48         break

小结:

  这个项目关键是先设计一个合理的架构。代码中的关键的流程控制语句包括:for..else..  if..else..  for .. in ..   if .. in..

正确的使用else语句可以参考一遍总结的不错的博文:http://www.codesky.net/article/201105/122398.html

if x in list:

这条语句会对list中的每一个元素进行遍历,判断x是否存在于list中,若存在就返回True。

腾飞前的蛰伏
原文地址:https://www.cnblogs.com/xiaoli2018/p/4460081.html