例子:
实现目标,用Python编写用户登录验证脚本。
知识点:
1、while和if控制流
2、运算表达式
验证过程:
脚本:
#!/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次,锁用户
2、登录成功
本文出自 “1058223494” 博客,请务必保留此出处http://4708705.blog.51cto.com/4698705/1430388