python 循環使用 while 或 for 語句實現用戶名密碼輸錯三次退出

若有錯誤歡迎你們指出,新手初來乍到。程序沒那麼複雜,是最簡單的。python

1、需求spa

編寫登陸文件 .py
1. 輸入用戶名密碼
2. 正確,輸出歡迎登陸
3. 當輸入用戶名和密碼小於 3 次,輸入用戶名或者密碼錯誤,提示用戶名或者密碼錯誤。再次輸入用戶名和密碼,剩餘輸入次
數。
3. 當輸錯三次後退出code

2、流程圖blog

 

3、代碼utf-8

forinput

 

#!/usr/bin/env python
#_*_conding:utf-8_*_



user = "zhangjinglei"
password = "lei100103"
count = 0;
for i in range(3):    
    username = input("username:")
    password = input("password:")
    if username == user and password == password:
        print("Welcome Login")
        count = 3
        break
    else:
        print("Wrong username or password")
        count += 1
        print("you can try", 2 - i, "times")

 

whileclass

#!/usr/bin/env python
#_*_conding:utf-8_*_



user = "zhangjinglei"
password = "lei100103"
count = 0;
while count < 3:    
    username = input("username:")
    password = input("password:")
    if username == user and password == password:
        print("Welcome Login")
        count = 3
    else:
        print("Wrong username or password")
        count += 1
        print("you can try", 3 - count, "times")

4、驗證結果
1.for 驗證結果登錄

2.whlie驗證結果程序

相關文章
相關標籤/搜索