2016-03-03 21 views
-4

我想弄清楚我的代碼在做什麼錯誤,無法正常工作。即使我在我的代碼中使用小寫,大寫或數字,它仍會打印出聲明,說我沒有。請幫忙!Python - 字符串,列表(密碼檢查器)

# function to check if the password is a valid one 
def validatePassword(userPassword): 

    # if the user has a lowercase character, then the password 
    # meets validation requirements 
    lowercase = "abcdefghijklmnopqrstuvwxyz" 
    if lowercase in userPassword: 
     hasLowercase = True 
    else: 
     hasLowercase = False 
     print("A lowercase letter is required. ") 

    # if the user has an uppercase character, then the password 
    # meets validation requirements 
    uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
    if uppercase in userPassword: 
     hasUppercase = True 
    else: 
     hasUppercase = False 
     print("An uppercase letter is required. ") 

    # if the user has a digit, then the password 
    # meets validation requirements 
    digits = "1234567890" 
    if digits in userPassword: 
     hasDigits = True 
    else: 
     hasDigits = False 
     print("A digit is required. ") 

    # if user enters a space, then the password 
    # does NOT meet validation requirements 
    whitespace = " " 
    if whitespace in userPassword: 
     hasWhitespace = True 
     print("Password must not contain a space.") 
    else: 
     hasWhitespace = False 

    # the user has successfully met all validation requirements 
    if ((hasLowercase == True) and (hasUppercase == True) and (hasDigits == True) and (hasWhitespace == False)): 
     return True 

# the user is inputting passwords  
def main(): 
    print("Please input passwords one-by-one for validation. ") 
    print("Enter END after you've entered your last password. ") 
    passwordList = [""] 
    while ("END" not in passwordList): 
     passwordList.append(str(input(""))) 
    del passwordList[-1] 
    for x in range(1,(len(passwordList))): 
     userPassword = passwordList[x] 

     # user has come up with a good password 
     if (validatePassword(userPassword) == True): 
      print("Password " + userPassword + " is good ") 

     # user has come up with a bad password 
     else: 
      print("Password " + userPassword + " is bad ") 

main() 
+0

請檢查'in'的工作方式。當你在word中說「if」asd時:'python檢查'asd'是否整個都在'word'中。單獨不是'a'或's'或'd'。 – Lafexlos

+0

我應該在每個單獨的字母之間使用逗號還是使其成爲列表? –

回答

0

當你說你if '123' in s檢查整個123字符串是一個字符串。不是每個人的性格。

更改爲:

if any((c in s) for c in '123') 

而且你的代碼將工作。 請注意,我建議您查看字符串方法upperlower,並查看模塊re(正則表達式)。這將幫助你爲這種情況編寫更好的代碼。

編輯:

在特定情況下,使用:

lowercase = "abcdefghijklmnopqrstuvwxyz" 
    if any((c in userPassword) for c in lowercase): 
+0

嗨,感謝您的回覆。所以,在我的情況下,它會是: if((小寫在userPassword)小寫'abc') –

+0

我很迷惑你所擁有的變量,c和s以及「any」 –

+0

我添加了可以粘貼到代碼中的確切行。對所有if語句使用相同的東西。任何只是一個內置函數。 – DevShark

0
請問

這對你的工作?

import getpass 
import string 

# function to check if the password is a valid one 
def validatePassword(userPassword): 

    # if the user has a lowercase character, then the password 
    # meets validation requirements 
    lowercase = list(string.ascii_lowercase) 
    hasLowercase = True 
    if not any([True if i in lowercase else False for i in userPassword]): 
     print("A lowercase letter is required. ") 
     hasLowercase = False 


    ## if the user has an uppercase character, then the password 
    ## meets validation requirements 
    uppercase = list(string.ascii_uppercase) 
    hasUppercase = True 
    if not any([True if i in uppercase else False for i in userPassword]): 
     print("An uppercase letter is required. ") 
     hasUppercase = False 

    ## if the user has a digit, then the password 
    ## meets validation requirements 
    digits = [str(i) for i in range(10)] 
    hasDigits = True 
    if not any([True if i in digits else False for i in userPassword]): 
     print("A digit is required. ") 
     hasDigits = False 

    ## if user enters a space, then the password 
    ## does NOT meet validation requirements 
    whitespace = " " 
    if whitespace in userPassword: 
     hasWhitespace = True 
     print("Password must not contain a space.") 
    else: 
     hasWhitespace = False 

    ## the user has successfully met all validation requirements 
    if ((hasLowercase == True) and (hasUppercase == True) and (hasDigits == True) and (hasWhitespace == False)): 
     return True 
    else: 
     return False 

if __name__ == '__main__': 
    print ("\nPlease input passwords one-by-one for validation\n(Type END after you've entered your last password): ") 
    passwordList = [] 

    while True: 
     current_password = getpass.getpass() # so user password doesn't display on screen, for security reasons. Use next line if you don't want this function. 
     #current_password = raw_input() 

     if "END" in current_password: 
      break 

     if current_password: 
      passwordList.append(str(current_password)) 

    for i, userPassword in enumerate(passwordList): 
     if validatePassword(userPassword): 
      result = "\nPassword " + str(i) + " is good " # change i to userPassword if you want to print password to screen, NOT RECOMMENDED 
     else: 
      result = "\nPassword " + str(i) + " is bad " 

     print(result)