1 def rand_code(n=6, alph=True): 2 code = '' 3 for i in range(n): 4 num = str(random.randint(0, 9)) # 随机产生一个随机整数 5 if alph: 6 alph_upper = chr(random.randint(65, 90)) # 随机产生大写字母 7 alph_lower = chr(random.randint(97, 122)) # 随机产生小写字母 8 num = random.choice([num, alph_upper, alph_lower]) 9 code += num 10 return code 11 12 13 print(rand_code(n=6))