1. Generate 9-letter password
Using the random.choice function, this function requires a sequence, so a given sequence contains A-Z, A-Z
#step1: Generating sequence import random s1='' for i in range(97,123): s1+=chr(i)+chr(i-32) #step2: Generate password password='' for i in range(9): password+=random.choice(s1)#from s1 Random selection of an element in a sequence print('9 The password for the number of digits is:',password)
2: generate a 9-digit number and letter password, which may appear randomly
On the basis of the previous question, this question becomes a sequence containing all letters and numbers, and then uses the random.choice() function
import random s1='' for i in range(97,123): s1+=chr(i)+chr(i-32) s1+='0123456789' print(s1) #Generate a sequence of lowercase letters and numbers #Another way of writing import string s1 = string.ascii_letters+string.digits password='' for i in range(9): password+=random.choice(s1) print('The random password is:',password)
3. Detect password strength
c1: length > = 8
c2: contains numbers and letters
c3: other visible special characters
Strong password condition: meet c1,c2,c3
Medium password condition: only any two conditions are met
Weak password condition: only 1 or 0 conditions are satisfied
Idea: first, write c1, c2 and c3 into functions, return True and False, True is satisfied, False is not satisfied
Step 1. Write three conditional functions
def check_len(password): if len(password)>=8: return True #Length greater than 8 True else: return False def check_letter_type(password): import string result =False for i in string.ascii_letters: #Case letters if i in password: #Password contains letters for j in string.digits: #number if j in password: #Number in password result =True return result def check_punctuation(password): import string result = False for i in string.punctuation: if i in password: #Password contains special characters result =True return result
Check clecheck password length, check letter type check whether the password contains letters and numbers, check punctuation check whether the password contains special characters
Step 2: check that the password meets several conditions
def pass_verify(password): count = 0 #Use count To record password Several conditions are satisfied if check_len(password): count +=1 if check_letter_type(password): count +=1 if check_punctuation(password): count +=1 if count==3: print("Strong cipher") elif count ==2: print('Middle password') else: print('Weak password') pass_verify(password)