2019-03-01python练习册,每天一个小程序之0001生成激活码

问题

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

处理代码

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


import string
import random

def coupon_creator(digit):#制作券码
    coupon=''#定义空字符串类型
    for word in range(digit):
        coupon+=random.choice(string.ascii_uppercase + string.digits)
    return coupon
    
def two_hundred_coupons():#制作券码集
    data=''
    count=1
    for count in range(200):
        digit=12
        count+=1
        data+='coupon no.'+str(count)+'  '+coupon_creator(digit)+'\n'#此处格式要学习
    return data

def main():#执行流程
    coupondata=open(r'E:\2019\py_practice\test4\coupondata.txt','w')
    coupondata.write(two_hundred_coupons())
    coupondata.close()

if __name__=='__main__':
    main()

代码执行结果

image.png

重要笔记

1 string 模块

https://www.cnblogs.com/qiuqiu64/p/10050617.html

string.ascii_lowercase  #打印所有的小写字母

string.ascii_uppercase  #打印所有的大写字母

string.ascii_letters #打印所有的大小写字母

string.digits  #打印0-9的数字

string.punctuation  #打印所有的特殊字符

string.hexdigits #打印十六进制的字符
string.printable  #打印所有的大小写,数字,特殊字符

2random模块

http://www.cnblogs.com/liangmingshen/p/8909376.html

1.random.random()

用于生成一个0到1的

随机浮点数:0<= n < 1.0

2.random.uniform(a,b) 

用于生成一个指定范围内的随机浮点数,两个参数其中一个是上限,一个是下限。如果a > b,则生成的随机数n: a <= n <= b。如果 a 

你可能感兴趣的:(2019-03-01python练习册,每天一个小程序之0001生成激活码)