编写登陆接口

  • 输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后锁定
1 #! /usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author : Orange   2018-01-24  python 3.x
 4 #登陆接口: 输入用户名密码,认证成功后显示欢迎信息,输错3次后锁定。-----多用户多密码未解决<br />
 5 
 6 import getpass
 7 _username = "orange"
 8 _password = "abc123"
 9 account_list = ["orange"]
10 lock_list = []
11 count = 0
12 
13 while True :
14     input_username = input("username:")
15 
16     # 判断当前所输入帐号是否在帐号列表中
17     if input_username in account_list:
18 
19         #判断当前所输入帐号是否在锁定帐号列表中,如果是,提示并退出.
20         if input_username in lock_list:
21             print ("%s account is locked !" % input_username)
22             break
23         else:
24             #如帐号密码匹配,显示登陆欢迎信息。
25             input_password = getpass.getpass("password:")
26             if _username == input_username and _password == input_password :
27                 print("Welcome user {name} login..." .format(name=_username))
28                 break
29 
30             #如帐号密码不匹配,则提示错误和警告,计数器启动,输入3次错误就将该帐号加入锁定列表中。
31             else:
32                 print("Wrong password!")
33                 count += 1
34                 print("You enter the wrong password %s times !" % count)
35                 print("If you enter the wrong password 3 times, account will be lock !")
36                 if count == 3:
37                     lock_list.append(input_username)
38 
39     #帐号不在列表中则提示无此帐号
40     else:
41         print("No {_name} account !" .format( _name = input_username))