第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?
知识点:
1、range生成随机数
2、int类型转换成char类型用chr() 函数
3、从list中随机取数,用random.sample()
def gen_code(length=8):
"""
将0~9,a~z,A~Z保存到list中,用random.sample从list中取固定位数
"""
code_list = []
for i in range(10):
code_list.append(str(i))
#print i
for i in range(65, 91):
code_list.append(chr(i))
#print chr(i)
for i in range(97, 123):
code_list.append(chr(i))
myslice = random.sample(code_list, length)
veri_code = ''.join(myslice)
return veri_code