# 用random模块生成随机验证码
def get_code(n=4): # 默认生成4位 # 定义一个空字符串 code = '' for i in range(n): # 将生成的随机整数转化为字符串 random_int = str(random.randint(0, 9)) # 0-9之间的整数 # 将ASCII编码转为字母 random_upper = chr(random.randint(65, 90)) random_lower = chr(random.randint(97, 122)) temp = random.choice([random_int, random_upper, random_lower]) code += temp return code # 生成6位随机验证码 print(get_code(6))