#-*- coding:utf-8 -*-
#第 0001 题: 做为 Apple Store App 独立开发者,
# 你要搞限时促销,为你的应用生成激活码(或者优惠券)
# ,使用 Python 如何生成 200 个激活码(或者优惠券)?
import random,string
def get_coupon(nums,lens):
# nums:生成总数
# lens:激活码长度
for x in range(nums):
coupon=random.sample(sam,lens)
coupon="".join(coupon) #合并
print(coupon)
write_in_file.writelines([coupon +'\n'])
if __name__=="__main__":
write_in_file = open("ex0003.txt", "w")
sam = string.ascii_letters + string.digits
get_coupon(200,20)
write_in_file.close()
重新写入了文件~#有关合并还有一个方法:http://hw1287789687.iteye.com/blog/2253173