python實(shí)現(xiàn)三次密碼驗(yàn)證的示例
需求:Python實(shí)現(xiàn)三次密碼驗(yàn)證,每次驗(yàn)證結(jié)果需要提示,三次驗(yàn)證不通過(guò)需要單獨(dú)提示
代碼如下:
user = ’張無(wú)忌’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 # 驗(yàn)證成功后更改confirm_flag,則不打印驗(yàn)證失敗提示break else:print(’Invalid user or password!’)if confirm_flag: print(’Input the invalid password more than three times’)
驗(yàn)證成功結(jié)果如下:
三次驗(yàn)證失敗結(jié)果如下:
上面代碼使用for-break循環(huán)、if/else的條件判斷來(lái)實(shí)現(xiàn)需求
三次驗(yàn)證失敗輸出提示部分代碼還可以?xún)?yōu)化,下面使用for-else循環(huán)優(yōu)化,代碼如下:
user = ’張無(wú)忌’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’)
驗(yàn)證成功結(jié)果如下:
三次驗(yàn)證失敗結(jié)果如下:
for/while循環(huán)之后的else語(yǔ)句,只有在循環(huán)正常結(jié)束后才會(huì)執(zhí)行,如果中間使用了break語(yǔ)句跳出循環(huán),則不會(huì)執(zhí)行
上面的代碼中,驗(yàn)證成功時(shí),通過(guò)break語(yǔ)句跳出了循環(huán),所以不會(huì)打印else之后的驗(yàn)證失敗語(yǔ)句,而三次驗(yàn)證未通過(guò)時(shí),循環(huán)正常結(jié)束,則會(huì)執(zhí)行else之后的提示語(yǔ)句
以上就是python實(shí)現(xiàn)三次密碼驗(yàn)證的示例的詳細(xì)內(nèi)容,更多關(guān)于python 密碼驗(yàn)證的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. React+umi+typeScript創(chuàng)建項(xiàng)目的過(guò)程2. .Net core 的熱插拔機(jī)制的深入探索及卸載問(wèn)題求救指南3. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp4. SharePoint Server 2019新特性介紹5. 三個(gè)不常見(jiàn)的 HTML5 實(shí)用新特性簡(jiǎn)介6. 解決ASP中http狀態(tài)跳轉(zhuǎn)返回錯(cuò)誤頁(yè)的問(wèn)題7. ASP中常用的22個(gè)FSO文件操作函數(shù)整理8. 無(wú)線標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)9. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過(guò)程解析10. ASP編碼必備的8條原則
