简明Python教程(四)―――用户登录验证

例子:

实现目标,用Python编写用户登录验证脚本。

知识点:

1、while和if控制流

2、运算表达式


验证过程:

wKiom1OpOprAyxBfAAEzEK3lXvk768.jpg

脚本:

#!/usr/bin/env python
#filename : User login authentication
#import sys
name = 'Tiger'
passwd = '123456'
counter = 0
times = 3
while True:                         #-----------无限循环
 username = raw_input('please input your username:').strip()
 if len(username) == 0:                #-----------判断为空
  print "please input username!"
  continue                                     #--------------------跳出单次循环
 elif username == name:
  pass                                       #------------------通过,没有改变
  break                                   #-------------------跳出if语句
 else:
  print "sorry,without this user,try again!"
 break                                     #------------------跳出while语句                                  
while True:
 password = raw_input("please input password of username:").strip()
 if len(password) == 0:
  print "password can't be empty,try again!"
  continue
 elif password == passwd:
  print "welcome,%s successful login!" % username
  break
 elif counter < 2:            #-----------计数器判断密码次数
  counter += 1               #-----------------用户输错一次密码,计数器自加1
  print "The password is wrong,but also to retry %s times" %(times-counter)
 elif counter == 2:
  print "Wrong password,the user is locked.10 minutes and try again!"
  break
  

执行结果:

1、超出3次,锁用户

wKioL1OpOOLx3UyqAAEHf9kMPvQ196.jpg

2、登录成功

wKiom1OpOVLxdn8uAACGCzaWoD8166.jpg

你可能感兴趣的:(while,python,if,strip())