The examples in this article share the specific code for judging Python password strength for your reference. The specific content is as follows
Program description: By obtaining user input, determine whether the password length is greater than 8, and at the same time determine whether it contains numbers and letters, and return relevant information.
【Related knowledge points】
**Python string: **
str.isnumeric() --- Check whether the string has only numbers and return True or False (note that there are only numbers)
str.isalpha() --- Check whether there are only letters in the string and return True or False
str.islower() --- Check whether the string is all lowercase
str.isupper() ——Check whether the string is all uppercase
"""
Author: Wang Xiao North
Date: 2019.05.19
Function: Determine the strength of the input password
Version: v2.0
Added function: Cycle termination
"""
# Determine whether the input string contains numbers
def existNumber(password_str):
has_number = False
for c in password_str:if c.isnumeric():
has_number = True
breakreturn has_number
# Generally, two returns are not used consecutively in the program
# return True #return terminates the loop early
# return False
# Determine whether the input string contains letters
# def existAlpha(password_str):
# for c in password_str:
# if c.isalpha():
# return True
# return False
# v2.0 Determine whether the input string contains letters
def existAlpha(password_str):
has_Alpha = False
for c in password_str:if c.isalpha():
has_Alpha = True
breakreturn has_Alpha
def main():"""
Main function
: return:12"""
Try_times =5while Try_times 0:
password =input('Please enter your password:')
# password strength
strength_level =0
# Rule 1: The password length is greater than 8iflen(password)=8:
strength_level +=1else:print('Please enter a password longer than 8...')
# Rule 2: Determine whether there are numbers
ifexistNumber(password):
strength_level +=1else:print('Password must contain numbers')
# Rule 3: The password contains letters
ifexistAlpha(password):
strength_level +=1else:print('Password must contain letters')if strength_level ==3:print('The password is entered correctly!')breakelse:
Try_times -=1if Try_times ==0:print('Too many incorrect passwords!')else:print('wrong password! Remaining{}Times'.format(Try_times))print() #Add blank line
if __name__ =='__main__':main()
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts