2017-10-08 38 views
-4

我試圖創建一個密碼檢查器,用戶被要求輸入8到24個字符的密碼(如果超出此範圍,將顯示一條錯誤消息)。此外,根據用戶輸入的密碼長度增加或減少點數。如何製作密碼檢查器?

如果至少有一個「大寫」,「小寫」,「符號」或「數字」: 加5分。

如果有一個大寫字母和一個數字,並且低一點:加15分。

如果輸入的密碼是'QWERTY'的形式:減去15分。

這裏是我到目前爲止的代碼:

passcheck = input("Enter a password to check: ") 

passlength = len(passcheck) 

symbols = {'!','$','%','^','&','*','(',')','-','_','=','+'} 
qwerty = ["qwertyuiop", "asdfghjkl", "zxcvbnm"] 

upper = sum(1 for character in passcheck if character.isupper()) 
lower = sum(1 for character in passcheck if character.islower()) 
num = sum(1 for character in passcheck if character.isnumeric()) 
sym = passcheck.count('!$%^&*()_-+=') 

if passlength <8 or passlength >24: 
    print("ERROR. Password must be between 8-24 characters long") 
else: 
    if upper in passcheck > 0: 
     score += 5 
    if lower in passcheck > 0: 
     score += 5 
    if num in passcheck > 0: 
     score += 5 

回答

0

你可以試試這個:

import sys 

passcheck = input("Enter a password to check: ") 

checking=set(passcheck) 

passlength = len(passcheck) 

points=0 

symbols = {'!','$','%','^','&','*','(',')','-','_','=','+'} 
qwerty = ["qwertyuiop", "asdfghjkl", "zxcvbnm"] 




if passlength <8 or passlength >24: 
    print("ERROR. Password must be between 8-24 characters long") 


else: 

    for i in qwerty: 

     if i in passcheck: #If entered password is in the form of 'QWERTY': subtract 15 points. 
      points-=15 
      print("your password contain form of 'QWERTY' , Don't use weak password.") 
      print(points) 
      sys.exit() 

if any(i.islower() for i in checking) or any(i.isupper() for i in checking) or any(i for i in checking if i in symbols) or any(i.isdigit() for i in checking): 
    points+=5 #If there is at least one 'capital', 'lower case', 'symbol' or 'number': add 5 points. 


if any(i.islower() for i in checking) and any(i.isupper() for i in checking) and any(i.isdigit() for i in checking): 
    points+=15 #If there is a capital and a number and a lower: add 15 points. 



print(points)