# 随机4位验证码 def yanzhengma(): strvar = "" for i in range(4): # 产生大写字母A~Z bchr = chr(random.randrange(65,91)) # 产生小写字母a~z schr = chr(random.randrange(97,123)) # 数字0~9 num = str(random.randrange(0,10)) # 把所有随机值得种类塞到列表里 listvar = [bchr,schr,num] # 随机选取一个 res = random.choice(listvar) # 拼接字符串 strvar += res # 返回字符串 return strvar res = yanzhengma() print(res)