久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

python實現(xiàn)三次密碼驗證的示例

瀏覽:4日期:2022-06-20 16:40:48

需求:Python實現(xiàn)三次密碼驗證,每次驗證結果需要提示,三次驗證不通過需要單獨提示

代碼如下:

user = ’張無忌’password = ’12345678’confirm_flag = Truefor 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’)

驗證成功結果如下:

python實現(xiàn)三次密碼驗證的示例

三次驗證失敗結果如下:

python實現(xiàn)三次密碼驗證的示例

上面代碼使用for-break循環(huán)、if/else的條件判斷來實現(xiàn)需求

三次驗證失敗輸出提示部分代碼還可以優(yōu)化,下面使用for-else循環(huán)優(yōu)化,代碼如下:

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’)

驗證成功結果如下:

python實現(xiàn)三次密碼驗證的示例

三次驗證失敗結果如下:

python實現(xiàn)三次密碼驗證的示例

for/while循環(huán)之后的else語句,只有在循環(huán)正常結束后才會執(zhí)行,如果中間使用了break語句跳出循環(huán),則不會執(zhí)行

上面的代碼中,驗證成功時,通過break語句跳出了循環(huán),所以不會打印else之后的驗證失敗語句,而三次驗證未通過時,循環(huán)正常結束,則會執(zhí)行else之后的提示語句

以上就是python實現(xiàn)三次密碼驗證的示例的詳細內(nèi)容,更多關于python 密碼驗證的資料請關注好吧啦網(wǎng)其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 临高县| 井研县| 孟津县| 尤溪县| 静乐县| 秭归县| 鲁甸县| 西林县| 和硕县| 鄂伦春自治旗| 司法| 武邑县| 商洛市| 衡南县| 牙克石市| 定日县| 沙洋县| 夏河县| 庐江县| 永登县| 柏乡县| 揭阳市| 维西| 抚顺县| 通化市| 聊城市| 鹤山市| 三亚市| 衡阳市| 商河县| 连平县| 江西省| 五指山市| 勐海县| 曲麻莱县| 海淀区| 凤冈县| 平凉市| 云龙县| 浦县| 丹东市|