Python登錄驗證小程序腳本(只是簡單的輸入驗證流程,無嚴格的字符輸入過濾判定)
腳本程序流程圖:
警告:使用此腳本前請先看清楚流程圖:筆者的判斷“輸入次數>3?”是對輸入用戶和密碼都爲非空後進行的判斷,別不看清楚流程就瞎判定不能退出。
倘若要對“輸入次數>3?”包括整個輸入過程(輸入爲空和非空):注意python對縮進很嚴格!可看下文login.py-v2.0 請在if語句下添加 retry_counter += 1 並修改相應的錯誤提示: print 'ERROR : Username cannot be empty! Only %s more chances!' % (3 - retry_counter )
login.py-v1.0:
#!/usr/bin/env python # _*_ coding:utf-8 _*_ #輸入以上一行纔可以識別中文字符註釋# # ------------------------------------------------------------------------------- # FileName: login.py # Revision: 1.0 # Date: 2017/09/20 # Author: XAD # Email: [email protected] # Website: http://onenice.blog.51cto.com/ # Description: check login # Notes: ~此版本計數器在判定輸入都爲非空後生效 # ------------------------------------------------------------------------------- # Copyright: 2017(c)XAD # License: GPL import sys username = 'xad' password = 'xad123' retry_counter = 0 #login_status = 'NO' login_status = 'YES' #用這個來模擬用戶是否已在線# # login verification part # while retry_counter <3 : #當retry_counter !在判定輸入都爲非空後才計數!大於3次則退出# user = raw_input('Username:').strip() if len(user) ==0: #驗證輸入用戶是否爲空,是則繼續循環,否則進入下一步驗證輸入# print "ERROR : Username cannot be empty!" continue passwd = raw_input('Password:').strip() if len(passwd) == 0: #驗證輸入密碼是否爲空,是則繼續循環,否則跳出循環# print "ERROR : Password cannot be empty!" continue else: if user == username and passwd == password: #驗證用戶密碼是否都正確,都正確才允許登錄!# if login_status == 'YES': sys.exit('Sorry, user %s has been logining!' % user ) else: sys.exit('Welcome %s logining to system!' % user ) else: #retry_counter = retry_counter + 1 retry_counter += 1 print 'Wrong username or password, please verify your username or password ! \nOnly %s more chances!' % (3 - retry_counter )
login.py-v2.0:
#!/usr/bin/env python # _*_ coding:utf-8 _*_ #輸入以上一行纔可以識別中文字符註釋# # ------------------------------------------------------------------------------- # FileName: login.py # Revision: 2.0 # Date: 2017/09/22 # Author: XAD # Email: [email protected] # Website: http://onenice.blog.51cto.com/ # Description: check login # Notes: ~計數器全程輸入生效 # ------------------------------------------------------------------------------- # Copyright: 2017(c)XAD # License: GPL import sys username = 'xad' password = 'xad123' retry_counter = 0 #login_status = 'NO' login_status = 'YES' #用這個來模擬用戶是否已在線# # login verification part # while retry_counter <3 : #當retry_counter 大於3次則退出# user = raw_input('Username:').strip() if len(user) ==0: #驗證輸入用戶是否爲空,是則繼續循環並計數,否則進入下一步驗證輸入# retry_counter += 1 print 'ERROR : Username cannot be empty! Only %s more chances!' % (3 - retry_counter ) continue passwd = raw_input('Password:').strip() if len(passwd) == 0: #驗證輸入密碼是否爲空,是則繼續循環並計數,否則跳出循環# retry_counter += 1 print 'ERROR : Password cannot be empty! Only %s more chances!' % (3 - retry_counter ) continue else: if user == username and passwd == password: #驗證用戶密碼是否都正確,都正確才允許登錄!# if login_status == 'YES': sys.exit('Sorry, user %s has been logining!' % user ) else: sys.exit('Welcome %s logining to system!' % user ) else: #retry_counter = retry_counter + 1 retry_counter += 1 print 'Wrong username or password, please verify your username or password ! \nOnly %s more chances!' % (3 - retry_counter )