每天一个小程序——生成激活码

练习地址:https://github.com/Yixiaohan/show-me-the-code

题目:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?

#! /usr/bin/env python
#-*- coding:utf-8 -*-

import random
import string

L = string.ascii_letters+string.digits

def generate_activtion_code(L,each_code_length=4):
    '''默认生成16位激活码,且将激活码每4位之间用'-'相连'''
    random_code = lambda x, y: ''.join([random.choice(L) for i in range(y)])
    return  '-'.join([random_code(L,each_code_length) for i in range(each_code_length)])

if __name__ == '__main__':
    # 生成200个随机码
    for i in range(200):
        print(generate_activtion_code(L))