# 写一个验证码
# 首先 要有数字
# 其次 要有字母
# 一共 4位
# 可以重复
import random
#普通版
new_num_l = list(map(str,range(10))) #['0','1'...'9']
alph_l = [] #用来存字母
for i in range(65,91):
alph = chr(i)
alph_l.append(alph) #['A'..'Z']
new_num_l.extend(alph_l)
ret_l = [] #存生成的随机数字或字母
for i in range(4):
ret_l.append(random.choice(new_num_l))
# ret = random.sample(new_num_l,4) #无法实现第四个要求
print(''.join(ret_l))
#优化版
def myrandom():
new_num_l = list(map(str,range(10)))
alph_l = [chr(i) for i in range(65,91)] #列表推导式
new_num_l.extend(alph_l)
ret_l = [random.choice(new_num_l) for i in range(4)]
return ''.join(ret_l)
print(myrandom())