2017-10-14 162 views
-1
def SignupDetails(): 

    Name= input("enter name: ") 
    Age= int(input("enter your age: ")) 
    yeargroup= input("Enter your year group: ") 
    username= Name[0:3]+str(Age) 
    print("username: ",username) 
    Password= input("please enter a password: ") 
    Password2= input("re-enter your password to confirm: ") 
    if Password != Password2: 
    print("the passwords do not match") 

我想創建一個while循環,這將使用戶在密碼和密碼2匹配前一直輸入密碼。任何幫助將不勝感激如何爲我的密碼創建一個while循環

+0

閱讀[凱文的回答](https://stackoverflow.com/a/23294659/918959),尤其是在標題**實施您自己的驗證規則**你呃讀別人也不會傷害。 –

回答

0

試試這個:

Password= input("please enter a password: ") 
Password2= input("re-enter your password to confirm: ") 

while Password != Password2: 
    print("the passwords do not match") 
    Password2= input("re-enter your password to confirm: ") 

編輯

可以擴展,以使用戶重新輸入第一密碼爲好,像這樣的東西:

Password= input("please enter a password: ") 
Password2= input("re-enter your password to confirm: ") 

while Password != Password2: 
    Password2 = input("The passwords do not match, hit enter to start again or re-enter your previous password to confirm: ") 
    if Password2 == "": 
     Password= input("please enter a password: ") 
     Password2= input("re-enter your password to confirm: ") 
+1

如果您錯誤輸入了第一個密碼,那麼這並不是很有用。 – efkin

+0

非常感謝!對不起,但有沒有什麼辦法可以調用這個函數,而不必在shell上寫SignupDetail()? – Coder777

+0

爲什麼?有什麼更優化或有效率,我可以使用? – Coder777

相關問題