我有一個密碼驗證器,我已經做到了,當少于 6 位數字時,將名為“invalid”的變數更改為 1,如果超過 16 位,則將名為“invalid”的變數更改為 1。如何做我讓它在變數為 1 時再次運行代碼,如果為 0 則停止。當無效變數為 1 時,有沒有辦法重新啟動代碼?
uj5u.com熱心網友回復:
初始化一個變數以檢查是否滿足條件。while 回圈將一直運行,直到密碼大小合適
check = 1
while check == 1:
input = input('Give your password')
if 6 < len(input) < 16:
check = 0
uj5u.com熱心網友回復:
您可以添加一些 if 陳述句。您可以使用“break”陳述句跳出回圈。這意味著當“break”被執行時,回圈將不再被執行。我不知道你的代碼是什么樣的,但你可以做這樣的事情。
if invalid == 0:
break
uj5u.com熱心網友回復:
invalid = True
while(invalid):
# your code here, accept/recheck password
if not(len(password)<6 or len(password)>16):
invalid = False # passw valid, breaks before next iteration
對于您的應用程式來說,True 和 False 似乎是 0 和 1 的更好替代方案,如果您選擇使用 0 和 1,它們仍然可以作業
uj5u.com熱心網友回復:
在閱讀您當前的代碼后,我會這樣做:
import re
print("""
Your password needs to have the following:
At least 1 lowercase character
At least 1 uppercase character
1 of these special characters ($#@)
Has to have a minium of 6 characters and a maximum of 16 characters
""")
invalid = 1
while invalid:
password = input("Please input a Password: ")
if len(password)<6 or len(password)>16:
print("Password length invalid, please try again")
elif not any(x in password for x in {"$","#","@"}):
print("Password must contain one of these special characters: $#@, please try again")
elif not re.search(r"[A-Z]", password) or not re.search(r"[a-z]", password):
print("Password must contain at least one capital and one lower case character, please try again")
else:
print("Password valid")
invalid = 0
結果:
Your password needs to have the following:
At least 1 lowercase character
At least 1 uppercase character
1 of these special characters ($#@)
Has to have a minium of 6 characters and a maximum of 16 characters
Please input a Password: hejhejhejhejhejhejhejhej
Password length invalid, please try again
Please input a Password: hej
Password length invalid, please try again
Please input a Password: hejhejhej
Password must contain one of these special characters: $#@, please try again
Please input a Password: hejhejhej#
Password must contain at least one capital and one lower case character, please try again
Please input a Password: Hejhejhej#
Password valid
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/446699.html
上一篇:用dplyr替換for回圈
