Python每天一个小程序(第1题)
题目要求:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?
思路:Apple Store App的激活码(优惠券)一般长度为10位,由小写字母与数字组成,因此需要用到Python中的random和string库。
代码:
1 2 3 4 5 6 7 8 9 |
import random,string def passwords(len): chars = string.ascii_letters.lower() + string.digits # return ''.join([random.choice(chars) for i in range(len)]) #得出的结果中会有重复 return ''.join(random.sample(chars,len)) if __name__ == '__main__': for j in range(200): print passwords(10) |