2015-04-01 80 views
-4

我如何製作一個python密碼程序,要求用戶輸入密碼並檢查密碼是否至少包含8個字符,一個數字,一個大寫字母和一個小寫字母。如果缺少其中一個,它將打印出一條聲明,告訴他們需要添加哪些內容才能確保密碼安全。 到目前爲止我的代碼:使用while循環在python 3.4中創建一個安全的密碼程序

incorrectPassword= True 
while incorrectPassword: 
    password = input("type in your password") 
    if len(password < 8): 
     print("your password must be 8 characters long") 
    elif noNum(password == False): 
     print("you need a number in your password") 
    elif noCap(password == False): 
     print("you need a capital letter in your password") 
    elif NoLow(password == False): 
+0

它爲我的計算機科學類的任務,我已經厭倦了做變量打印出來的時候在一個密碼,但IM的用戶類型停留在如何使它檢查超過8個字符,如果它有一個大寫字母。這是分配 – 2015-04-01 23:29:43

+0

編輯你的問題,包括你已經嘗試過的代碼以及它失敗的方式(錯誤的結果,錯誤信息等)。 – TigerhawkT3 2015-04-01 23:38:00

+0

你可以讀取該代碼嗎?我不能,請修復它。 – MrSykkox 2015-04-01 23:46:43

回答

-2

要檢查密碼字符串包含一個數字,大寫字母或類似的,你可以使用正則表達式,這裏是一個很好的工具來測試和學習[RegExr(http://www.regexr.com/)。

這是一個很好的做法,有一個函數將檢查,驗證您的輸入,如果你需要做多次e.q.在一個while循環中。

import re 

def checkPassword(password): 
    """ 
    Validate the password 
    """ 
    if len(password) < 8: 
     # Password to short 
     print("Your password must be 8 characters long.") 
     return False 
    elif not re.findall(r'\d+', password): 
     # Missing a number 
     print("You need a number in your password.") 
     return False 
    elif not re.findall(r'[A-Z]+', password): 
     # Missing at least one capital letter 
     print("You need a capital letter in your password.") 
     return False 
    else: 
     # All good 
     print("All good") 
     return True 

# Promt user to enter valid password 
passwordValid = False 
while not passwordValid: 
    password = input("Type in your password: ") 
    passwordValid = checkPassword(password) 
+2

正則表達式對此非常嚴重的矯枉過正。 – TigerhawkT3 2015-04-01 23:59:06

+0

@ TigerhawkT3你說得對,但由於我們談論的是8個字符的字符串,這並不是那麼嚴重。例如,你可以降低字符串,並檢查兩個匹配(檢查大寫字母)。 – 2015-04-02 00:08:49

+1

對短字符串進行簡單檢查的事實是嚴重矯枉過正的原因。 Python有幾個基本的功能和方法,對於像這樣的問題是理想的。我只希望這個問題不適用於學校作業,因爲如果提問者使用這些代碼,教授會知道一些事情已經結束。 – TigerhawkT3 2015-04-02 00:12:37

0

len(password - 8)首先嚐試比較8到用戶輸入的密碼,這是行不通的。您必須首先找到密碼的長度,然後將其與8len(password) - 8進行比較。

noNum(password == False)之類的東西會發送一個布爾值到noNum()函數我推測你有某處,這可能不是它所需要的。您可能需要noNum(password) == True - 因爲如果傳遞的字符串不包含數字,noNum()應返回True

也就是說,Python具有確定字符串是大寫,小寫還是數字(僅包含數字)的字符串方法。使用這些來評估給定的密碼以及any()函數。

incorrectPassword= True 
while incorrectPassword: 
    password = input("type in your password: ") 
    if len(password) < 8: 
     print("your password must be 8 characters long") 
    elif not any(i.isdigit() for i in password): 
     print("you need a number in your password") 
    elif not any(i.isupper() for i in password): 
     print("you need a capital letter in your password") 
    elif not any(i.islower() for i in password): 
     print("you need a lowercase letter in your password") 
    else: 
     incorrectPassword = False 
+0

在你的情況下,任何與檢查isupper/islower/isdigit比正則表達式慢。 (time).timeit('any(i.isupper)')運行在1.1296026709896978,其中'timeit.timeit('re.findall(r「[AZ] +」,「asdfqweR」)',setup =「import re」 ()爲我在「asdfqweR」)')'運行在1.2718868080119137 – 2015-04-02 00:20:06

+0

你熟悉短語「過早優化是萬惡之源」嗎?此外,我用'perf_counter()'測試了每個版本,而我的測試需要1-5分之一毫秒,而你的測試需要7-8個毫秒,對於幾個字符和幾百個字符之間的字符串。 – TigerhawkT3 2015-04-02 00:29:16

0
while True : 
     name = input('Enter user name') 
     if name != 'leela': 
       continue 
     password = input('hello,leela Enter password') 
     if password='sana': 
       break 
print('Access granted') 
+0

如果名稱包含四位數字並且密碼僅包含四位數字 – 2017-09-13 04:56:34

+0

那麼我們將執行該程序 – 2017-09-13 04:57:40