方法一:
1 import random 2 def generate_code(): 3 code_list=[] 4 for i in range(10): 5 code_list.append(str(i)) 6 for i in range(65,91): 7 code_list.append(chr(i)) 8 for i in range(97,123): 9 code_list.append(chr(i)) 10 mystlic=random.sample(code_list,6) 11 ver_code=‘‘.join(mystlic) 12 return ver_code
方法二:
def generate_verification_code2(): ‘‘‘ 随机生成6位的验证码 ‘‘‘ code_list = [] for i in range(2): random_num = random.randint(0, 9) # 随机生成0-9的数字 # 利用random.randint()函数生成一个随机整数a,使得65<=a<=90 # 对应从“A”到“Z”的ASCII码 a = random.randint(65, 90) b = random.randint(97, 122) random_uppercase_letter = chr(a) random_lowercase_letter = chr(b) code_list.append(str(random_num)) code_list.append(random_uppercase_letter) code_list.append(random_lowercase_letter) verification_code = ‘‘.join(code_list) return verification_code
原文地址:https://www.cnblogs.com/lishun412/p/9231338.html
时间: 2024-10-13 13:25:54