随机n 位验证码
第一种纯数字六位验证码
import random
def get_code(n=6):
s = ''
for i in range(n):
num = random.randint(0,9)
s+=str(num)
return s
ret=get_code()
print(ret)
#第二种方法 随机验证码 包括数字 字母等
# import random
#
# lst = []
# for i in range(48,59):
# lst.append(chr(i))
#
# for i in range(ord("a"), ord("z") + 1):
# lst.append(chr(i))
#
# for i in range(ord('A'), ord('Z') + 1):
# lst.append(chr(i))
#
# print(random.sample(lst,6))
第三种 所有格式全部通用
# import random
# def code(n=4):
# end = ''
# for i in range(n):
# num = str(random.randint(0,9))
# alpha_up = chr(random.randint(65,90))
# alpha_low = chr(random.randint(97,122))
# aim = random.choice([num,alpha_up,alpha_low])
# end += aim
# return end
# print(code())
#
# import random
# def code(n=4,alpha = True):
# end = ''
# for i in range(n):
# aim = str(random.randint(0,9))
# if alpha:
# alpha_up = chr(random.randint(65,90))
# alpha_low = chr(random.randint(97,122))
# aim = random.choice([aim,alpha_up,alpha_low])
# end += aim
# return end
# print(code(n=6,alpha=False))
# print(code())