# 示例一
import random # 导入一个模块 random:随机 data = [] for i in range(4): # 4位验证码 v = random.randint(78,89) # 验证码的随机范围是78 ~ 89 . data.append(v) print(data) # [81, 84, 82, 87] ------------------------------------ # 示例二
import random data = [] for i in range(4): v = random.randint(78,89) data.append(chr(v)) # chr() :将十进制转换成unicode编码中的对应字符串 print(data) # ['y','s','o','s'] print(''.join(data)) # YSOS ------------------------------------------ # 示例三
import random def get_random_code(length=6): #默认验证码长度等于6 data = [] for i in range(length): v = random.randint(78, 89) data.append(chr(v)) return ''.join(data) code = get_random_code() print(code) # QWVWXO (该验证码随机生成)