1 def code():2 from random import randint3 num =[str(i) for i in range(0,10)] #生成0-9的列表4 low_alph = [chr(i) for i in range(97,123)] #生成a-z小写字母列表5 upp_alph = [chr(j) for j in range(65,91)] #生成A-Z大写字母列表6 lst = num + low_alph + upp_alph #将所有元素添加到一个列表7 8 code = random.sample(lst,4) #从列表中所有字符中,抽取四个9 return code
1 def code():2 import random3 msg = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXZ"4 code = ""5 for i in range(4):6 code += msg[random.randint(0,55)]7 print(code)