python实现三次密码验证的示例
作者:弓长无忌 时间:2022-01-24 16:34:25
需求:Python实现三次密码验证,每次验证结果需要提示,三次验证不通过需要单独提示
代码如下:
user = '张无忌'
password = '12345678'
confirm_flag = True
for i in range(0, 3):
user_input = input('user:')
password_input = input('password:')
if user_input == user and password_input == password:
print('Welcome! %s' % user)
confirm_flag = False # 验证成功后更改confirm_flag,则不打印验证失败提示
break
else:
print('Invalid user or password!')
if confirm_flag:
print('Input the invalid password more than three times')
验证成功结果如下:
三次验证失败结果如下:
上面代码使用for-break循环、if/else的条件判断来实现需求
三次验证失败输出提示部分代码还可以优化,下面使用for-else循环优化,代码如下:
user = '张无忌'
password = '12345678'
for i in range(0, 3):
user_input = input('user:')
password_input = input('password:')
if user_input == user and password_input == password:
print('Welcome! %s' % user)
break
else:
print('Invalid user or password!')
else:
print('Input the invalid password more than three times')
验证成功结果如下:
三次验证失败结果如下:
for/while循环之后的else语句,只有在循环正常结束后才会执行,如果中间使用了break语句跳出循环,则不会执行
上面的代码中,验证成功时,通过break语句跳出了循环,所以不会打印else之后的验证失败语句,而三次验证未通过时,循环正常结束,则会执行else之后的提示语句
来源:https://www.cnblogs.com/programmer4027/p/14715995.html
标签:python,密码,验证
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IE7的web标准之道 Ⅰ
2008-08-13 12:42:00
![](https://img.aspxhome.com/file/UploadPic/20088/13/200881312500620s.gif)
如何利用Python模拟GitHub登录详解
2023-11-18 11:08:05
![](https://img.aspxhome.com/file/2023/4/64354_0s.png)
典型的三行二列居中高度自适应css布局
2008-02-22 16:02:00
![](https://img.aspxhome.com/file/UploadPic/20082/22/2008222164924132s.gif)
在PHP3中实现SESSION的功能(二)
2023-11-17 11:26:52
numpy中的ndarray方法和属性详解
2021-04-17 10:19:04
golang copy函数使用的坑
2023-07-09 19:53:44
ajax实现Dig程序中的投票
2008-01-22 17:27:00
![](https://img.aspxhome.com/file/UploadPic/20081/22/2008122173320740s.jpg)
MySQL安全性指南 (1)(转)
2010-07-27 12:49:00
关于网站地图
2011-01-06 12:14:00
![](https://img.aspxhome.com/file/UploadPic/20111/6/116-64s.jpg)
SQL Server小知识:Processor Affinity
2008-11-24 20:50:00
asp select下拉菜单选择图标并实时显示
2011-04-03 10:33:00
js查找父节点的简单方法
2023-09-11 01:12:42
python将文本转换成图片输出的方法
2023-06-20 11:47:47
Python中selenium库的用法详解
2021-06-26 00:29:41
方便和实用
2009-02-02 10:16:00
用户体验量化方法研究(二)
2010-02-08 12:46:00
图片滤镜效果[IE Only]
2009-06-14 19:49:00
对python添加模块路径的三种方法总结
2023-11-26 04:01:35
php将12小时制转换成24小时制的方法
2023-11-21 15:56:08
Python socket C/S结构的聊天室应用实现
2023-08-01 05:06:38