1. 程式人生 > >python從入門到放棄-day05-用戶登錄(for , if ,break)

python從入門到放棄-day05-用戶登錄(for , if ,break)

int 需要 入門 == log true 入門到 %d word

#   作者 : liuxing
#   日期 : 2017-12-25
#   通過for ,if,break實現用戶登錄驗證

_name="liuxing"
_password="8888"
passed=False        #驗證是否通過
times=3             #重試次數
for i in range(times):
    name = input("name=")
    password = input("password=")
    if name==_name and password==_password:
        passed=True
        
print("歡迎 %s 驗證通過!!" % name) break else: print("用戶名或密碼錯誤!,請重試還有%d次機會。。。" % (times-i-1)) if passed==False: print("用戶登錄已被鎖死,請4小時以後重試!")
結果:
name=liuxing
password=1234
用戶名或密碼錯誤!,請重試還有2次機會。。。
name=liuxing
password=8888
歡迎 liuxing 驗證通過!!

通過while: else實現上面的功能 ,不再需要變量(passed)判斷驗證是否通過

#   作者 : liuxing
#   日期 : 2017-12-25
#   for :else

_name="liuxing"
_password="8888"
times=3             #重試次數
for i in range(times):
    name = input("name=")
    password = input("password=")
    if name==_name and password==_password:
        print("歡迎 %s 驗證通過!!" % name)
        break
    else:
        
print("用戶名或密碼錯誤!,請重試還有%d次機會。。。" % (times-i-1)) else: #僅在for循環正常退出才執行 print("用戶登錄已被鎖死,請4小時以後重試!")

python從入門到放棄-day05-用戶登錄(for , if ,break)