簡體   English   中英

該變量在我的方法中設置為 false,一旦方法運行,它應該更改為 True。 但它沒有

[英]the variable is set to false inside my method, once the method runs it should change to True. But it does not

def checkPassword (password):
    
    upper = False
    lower = False
    number = False
    special = False
    
    for i in range (len(password)):
        if ((ord(password[i]) >= 65) and (ord(password[i]) <= 90)):
            upper = True    
        elif ((ord(password[i]) >= 97) and (ord(password[i]) <= 122)):
            lower = True    
        elif ((ord(password[i]) >= 0) and (ord(password[i]) <= 9)):
            number = True   
        elif ((ord(password[i]) >= 0) and (ord(password[i]) <= 9)) :
            special = True
            
    
    if ((upper == True) and (lower == True) and (number == True) and (special == True)):
        print ("Your password is strong")
    else:
        print ("Your password is not strong. Make sure to make it a mix between upper and lower case letters, number, and special characters.")

def main():
    password = input("Enter a password: ")
    checkPassword (password)

輸入:

running99*FAST

Output:

Your password is not strong. Make sure to make it a mix between upper and lower case letters, number, and special characters.

問題:這個密碼應該作為強密碼返回,但它沒有。 老實說,我不知道我做錯了什么。

PS 我正在使用 ASCII 表來識別字母是小寫、大寫還是數字以及特殊字符。

您的電話號碼和特別支票無效。 所以我用isdigit()代替數字。 另外,什么是特殊字符? 如果特殊字符不是小寫/大寫字符和數字,那么最后的else應該沒問題。

upper = False
lower = False
number = False
special = False

password = "running99*FAST"

for i in range (len(password)):
    if ((ord(password[i]) >= 65) and (ord(password[i]) <= 90)):
        upper = True    
    elif ((ord(password[i]) >= 97) and (ord(password[i]) <= 122)):
        lower = True    
    elif password[i].isdigit():
        number = True   
    else:
        special = True
        
print (upper,lower,number,special)
if ((upper == True) and (lower == True) and (number == True) and (special == True)):
    print ("Your password is strong")
else:
    print ("Your password is not strong. Make sure to make it a mix between upper and lower case letters, number, and special characters.")

Output:

True True True True
Your password is strong

對於正則表達式版本:

import re

password = "running99*FAST"

def check(password):
    for test in [('lower','[a-z]'),('upper','[A-Z]'),('number','[0-9]'),('special','[^A-Za-z0-9]')]:
        if len(re.sub(test[1],"",password)) == len(password):
            return False, test[0]
    else: return True

print (check(password))

如果正則表達式替換后的長度等於替換前的長度(意味着不存在字符類型),此代碼將返回 False。 一次違規返回。

以及報告多次違規的 oneliner:

import re

password = "runnFAST"

def check(password):
    return [(False, t[0]) if len(re.sub(t[1],"",password)) == len(password) else (True, t[0]) for t in [('lower','[a-z]'),('upper','[A-Z]'),('number','[0-9]'),('special','[^A-Za-z0-9]')]]

print (check(password))

Output:

[(True, 'lower'), (True, 'upper'), (False, 'number'), (False, 'special')]

正如其他人所說,您應該查看ord()為每個字符返回什么(提示,查看它應該為 0-9 返回什么)。 最簡單的方法是在每個循環步驟的開頭使用print(ord(password[i]))

還,

elif ((ord(password[i]) >= 0) and (ord(password[i]) <= 9)):
  number = True   
elif ((ord(password[i]) >= 0) and (ord(password[i]) <= 9)):
  special = True

您不需要兩次比較。 如果這是您想要的邏輯,您可以為相同的條件設置numberspecial True。

最后,您應該考慮根本不使用ord()來代替其他方法來判斷 char 是大寫、小寫還是數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM