Python练习题第 0001 题
https://github.com/Show-Me-the-Code/show-me-the-code
第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?
我的代码生成200个,长度为8的随机码
Talk is cheap, show you my code.
#! /usr/bin/env python
#! -*- coding:utf-8 -*-
import random, string
__author__ = 'Sophie'
def randomSequence(r,l):
s = string.letters + string.digits + '@#$%&*'
random_seq = []
# Method_1
#for i in range(r):
# random_seq.append(''.join(random.sample(s,l)))
#return random_seq
# Method_2
sl = list(s)
print sl
for i in range(r):
random.shuffle(sl)
random_seq.append(''.join(sl[:l]))
return random_seq
if __name__ == '__main__':
result = randomSequence(200,8)
for i in range(len(result)):
print result[i]
小知识点Get
MAC下查看API一大利器Dash~~
另外附上一位仁兄的random模块介绍
http://blog.csdn.net/jgood/article/details/4278885