Python Password Checker -
i need figuring out how make function checks string bunch of conditions.
passwords must @ least 5 characters long
passwords must contain @ least 1 upper case letter
passwords must contain @ least 2 numbers
passwords may not contain characters "e" or "e"
passwords must include @ least 1 special symbol: !, @, #, $, %, ^, &
right have
def passwordchecker(password): ''' ''' caps = sum(1 c in password if c.isupper()) nums = sum(1 c in password if c.isdigit()) symb = any(c in password c in '!@#$%^&') note = any(c in password c in 'ee') if len(password) <5: return false elif caps < 1: return false elif nums < 1: return false elif symb == false: return false else: return true edit**
just realized have check if there commonly used passwords 'password' or '111111' , dont know how approach this.
just alternative using regular expressions:
import re def passwordchecker(password): return all(re.search(pattern, password) pattern in ('.{5}', '[a-z]', '\d.*\d', '^[^ee]*$', '[!@#$%^&]')) demo using 5 barely invalid , 5 barely valid tests (one invalid , 1 valid each of 5 rules):
for password in ('1a!', '12!34', 'a1bc!', 'a12e!', 'a12bc', '1a!2.', 'a2!34', 'a12c!', 'a12b!', 'a12b@'): print(passwordchecker(password)) prints false first 5 , true last five.
Comments
Post a Comment