2013-11-22 306 views
3

我想創建一個用戶在控制檯中輸入密碼的小程序。然後,程序根據用戶輸入的內容檢查密碼是弱,中或強。Python - 檢查數字,大寫,小寫字母和特殊字符的輸入

我需要檢查輸入了多少個大寫,小寫和數字,然後告訴用戶密碼的強度。

我已經完成了大部分程序,但由於我沒有真正使用過Python,所以我不太熟悉任何先進的東西,請記住,年輕人也必須理解這些代碼,這些代碼不會很好在編碼自己。

到目前爲止,我有:

#Welcome the user to the application. 
print("Hello, please enter a password to check how secure it is"); 

#Set a variable called MinPass and set a value of 6. 
MinPass = 6; 

#Set a variable called MaxPass and set a value of 12. 
MaxPass = 12; 

#Set variable EnteredPass and wait for user input 
EnteredPass = input("Password: "); 

while len(EnteredPass) < MinPass: 
    print("Your password is too short, please enter a longer password and try again") 
    EnteredPass = input("Password: "); 

while len(EnteredPass) > MaxPass: 
    print("Your password is too long, please shorten it and try again!"); 
    EnteredPass = input("Password: "); 

請注意,這僅僅是爲了教育目的。我不打算製作一個旨在竊取隨機密碼的程序。這是我校學習課程的一部分!

+2

你還沒有做出很多嘗試,你呢?你有沒有看過['str'類型方法](http://docs.python.org/2/library/stdtypes.html#string-methods)? –

+1

@MartijnPieters他們已經設置了一些變量:)我只是想知道什麼是「womabt」是:) –

+0

雖然,我已經回答了您的問題,並且有更多的人可以回答這些問題,我會建議您在提問關於SO的問題之前,先看一下python的文檔(或者你正在學習的語言)。即使是最好的程序員,也要繼續參考語言的文檔。另外,python提供的文檔非常好。 –

回答

4

這包含了一些更高級的概念,但應該很容易遵循:

import string 

def long_enough(pw): 
    'Password must be at least 6 characters' 
    return len(pw) >= 6 

def short_enough(pw): 
    'Password cannot be more than 12 characters' 
    return len(pw) <= 12 

def has_lowercase(pw): 
    'Password must contain a lowercase letter' 
    return len(set(string.ascii_lowercase).intersection(pw)) > 0 

def has_uppercase(pw): 
    'Password must contain an uppercase letter' 
    return len(set(string.ascii_uppercase).intersection(pw)) > 0 

def has_numeric(pw): 
    'Password must contain a digit' 
    return len(set(string.digits).intersection(pw)) > 0 

def has_special(pw): 
    'Password must contain a special character' 
    return len(set(string.punctuation).intersection(pw)) > 0 

def test_password(pw, tests=[long_enough, short_enough, has_lowercase, has_uppercase, has_numeric, has_special]): 
    for test in tests: 
     if not test(pw): 
      print(test.__doc__) 
      return False 
    return True 

def main(): 
    pw = input('Please enter a test password:') 
    if test_password(pw): 
     print('That is a good password!') 

if __name__=="__main__": 
    main() 
+1

您的解決方案似乎工作得最好。我已經測試過它,這就是我所追求的,我可以相當瞭解它!謝謝。 –

0

您可以使用以下功能: -

isalnum() # To check for alpha numeric characters 
    isalpha() # To check for the presence of only alphabets 

爲如。

password = input("Enter the password: ") 
    if password.isalnum() == False: 
     print "Password should contain atleast one special character or number" 
    if password.isalpha() == False: 
     print "Password should contains atleast some alphabets." 

要檢查的資本,或者你可以使用這個小字母的存在: -

temp = passsword.lower() 
    if temp == password: 
     print "Password should contain atleast one capital letters" 

如何運作? 儘管此代碼是不言自明的,有關於它沒有什麼火箭科學,我將解釋它,因爲,你似乎是一個初學者對我說: -

str.isalnum() returns False if the string contains some non alphabet characters 
    str.isalpha() returns false if the string does not contain a single alphabet 

通過創建

temp 

我正在存儲一個小寫密碼的副本。因此,如果密碼變量包含一些大寫字母則

temp == password 

將返回FALSE,你可以找出字符串是否包含某些大寫字母或沒有。 正如指出的@MartijnPieters,您可以使用

if not password.isalnum(): 

代替

if password.isalnum() == False: 

雖然還可以,第一種方式是更「Python的」

+1

這似乎是一個合適的解決方案...謝謝!我現在會試試這個,並且告訴你它是如何工作的! –

+1

不*測試'==假'。使用'if not password.isalnum():'代替。 –

+0

感謝@MartijnPieters的建議。我已在最終答案中添加了您的建議。 –

-1

寫MATLAB代碼爲了檢查輸入的密碼的強度,代碼應該輸出:'弱','中等','強'和非常強',這取決於密碼的長度,使用大寫字母和小寫字母,使用數字和特殊字符。

+1

歡迎來到SO!這並沒有真正回答OP *如何檢查密碼的問題。 – zett42

相關問題