1 def code(): 2 from random import randint 3 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 random 3 msg = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXZ" 4 code = "" 5 for i in range(4): 6 code += msg[random.randint(0,55)] 7 print(code)