python练习册 每天一个小程序 第0001题
1 # -*-coding:utf-8-*-
2 __author__ = 'Deen'
3 '''
4 题目描述:
5 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?
6 '''
7 '''
8 import random
9
10
11 '''
12 '''
13 import string
14 import random
15
16
17 def coupon_creator(digit):
18 coupon = ''
19 for word in range(digit):
20 coupon += random.choice(string.ascii_uppercase + string.digits)
21 return coupon
22
23
24 def two_hundred_coupons():
25 data = ''
26 count = 1
27 for count in range(200):
28 digit = 12
29 count += 1
30 data += 'coupon no.' + str(count) + ' ' + coupon_creator(digit) + '\n'
31
32 return data
33
34
35 coupondata = open('coupondata.txt', 'w')
36 coupondata.write(two_hundred_coupons())
37 '''
38 '''
39 >>> import string
40 >>> string.ascii_letters
41 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
42 >>> string.ascii_lowercase
43 'abcdefghijklmnopqrstuvwxyz'
44 >>> string.ascii_uppercase
45 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
46 >>> string.digits
47 '0123456789'
48 >>> string.hexdigits
49 '0123456789abcdefABCDEF'
50 >>> string.letters
51 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
52 >>> string.lowercase
53 'abcdefghijklmnopqrstuvwxyz'
54 >>> string.uppercase
55 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
56 >>> string.octdigits
57 '01234567'
58 >>> string.punctuation
59 '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
60 >>> string.printable
61 '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
62 >>> string.whitespace
63
64 '''
65 '''
66 random.random()用于生成
67 用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。如果a > b,则生成随机数
68 ?
69 1
70 n: a <= n <= b。如果 a <b, 则 b <= n <= a。
71 ?
72 1
73 2
74 3
75 4
76 5
77 6
78 print random.uniform(10, 20)
79 print random.uniform(20, 10)
80 #----
81 #18.7356606526
82 #12.5798298022
83 random.randint
84 用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,Python生成随机数
85 ?
86 1
87 2
88 3
89 print random.randint(12, 20) #生成的随机数n: 12 <= n <= 20
90 print random.randint(20, 20) #结果永远是20
91 #print random.randint(20, 10) #该语句是错误的。
92 下限必须小于上限。
93 random.randrange
94 从指定范围内,按指定基数递增的集合中 ,这篇文章就是对python生成随机数的应用程序的部分介绍。
95 随机整数:
96 >>> import random
97 >>> random.randint(0,99)
98 21
99 随机选取0到100间的偶数:
100 >>> import random
101 >>> random.randrange(0, 101, 2)
102 42
103 随机浮点数:
104 >>> import random
105 >>> random.random()
106 0.85415370477785668
107 >>> random.uniform(1, 10)
108 5.4221167969800881
109 随机字符:
110 >>> import random
111 >>> random.choice('abcdefg&#%^*f')
112 'd'
113 多个字符中选取特定数量的字符:
114 >>> import random
115 random.sample('abcdefghij',3)
116 ['a', 'd', 'b']
117 多个字符中选取特定数量的字符组成新字符串:
118 >>> import random
119 >>> import string
120 >>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
121 eplace(" ","")
122 'fih'
123 随机选取字符串:
124 >>> import random
125 >>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
126 'lemon'
127 洗牌:
128 >>> import random
129 >>> items = [1, 2, 3, 4, 5, 6]
130 >>> random.shuffle(items)
131 >>> items
132 [3, 2, 5, 6, 4, 1]
133 '''
134
135 # 改进
136
137 import string
138 import random
139
140 # n为生成的个数,length为生成的长度
141 def get_poll_codes(n, length):
142 poll_codes = list()
143 for i in range(n):
144 poll_code = ''
145 for j in range(length):
146 poll_code += random.choice(string.uppercase + string.digits)
147 if poll_code in poll_codes:
148 pass
149 else:
150 poll_codes.append(poll_code)
151 n = 0
152 with open('poll_codes.txt', 'w') as fp:
153 for i in poll_codes:
154 n += 1
155 fp.write(str(n) + ':' + i + '\n')
156 fp.close()
157
158
159 if __name__ == '__main__':
160 get_poll_codes(200, 10)
python练习册 每天一个小程序 第0001题的更多相关文章
- python练习册 每天一个小程序 第0013题
# -*-coding:utf-8-*- ''' 题目描述: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-) 地址: http://tieba.baidu.com/p/21 ...
- python练习册 每天一个小程序 第0007题
1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但 ...
- python练习册 每天一个小程序 第0000题
PIL库学习链接:http://blog.csdn.net/column/details/pythonpil.html?&page=1 1 #-*-coding:utf-8-*- 2 __au ...
- python练习册 每天一个小程序 第0010题
# -*-coding:utf-8-*- ''' 题目描述: 使用 Python 生成类似于下图中的字母验证码图片 思路: 运用PIL库加random 随机字母进行生成 ''' import rand ...
- python练习册 每天一个小程序 第0009题
1 ''' 2 题目描述: 3 找出一个html文件中所有的url 4 5 思路 : 6 利用正则表达式进行匹配 7 8 ''' 9 10 11 import re 12 13 14 with ope ...
- python练习册 每天一个小程序 第0008题
1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 一个HTML文件,找出里面的正文. 6 7 思路: 8 利用Beautiful ...
- python练习册 每天一个小程序 第0006题
1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都 ...
- python练习册 每天一个小程序 第0005题
1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目说明: 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小 ...
- python练习册 每天一个小程序 第0012题
# -*-coding:utf-8-*- def test(content): text = content flag = 0 with open('filtered_words.txt') as f ...
随机推荐
- Netty核心原理
Netty核心原理 1. Netty介绍 1.1 原生NIO存在的问题 NIO的类库和API使用繁杂 需要具备其他额外的技能,如java多线程编程等才能编写出高质量的NIO程序 开发工作量和难度都非常 ...
- Solution -「多校联训」消失的运算符
\(\mathcal{Description}\) Link. 给定长度为 \(n\) 的合法表达式序列 \(s\),其中数字仅有一位正数,运算符仅有 - 作为占位.求将其中恰好 \(k\) ...
- Diary -「CSP 2019 J/S」 游记
\(\text{Day 0}\) 试机, 总体感觉不错, 至少不像初一时候的紧张, 毕竟是中青年选手了 ( ? ) 当晚睡得挺好, 虽然是冲着一等奖去的, 但还是没有给自己过多的思想包 ...
- Solution -「ARC 110E」Shorten ABC
\(\mathcal{Description}\) Link. 给定长度为 \(n\),包含 A, B, C 三种字符的字符串 \(S\),定义一次操作为将其中相邻两个不相同的字符替换为字符集 ...
- pytest(12)-Allure常用特性allure.attach、allure.step、fixture、environment、categories
上一篇文章pytest Allure生成测试报告我们学习了Allure中的一些特性,接下来继续学习其他常用的特性. allure.attach allure.attach用于在测试报告中添加附件,补充 ...
- [Java]程序运行时的内存分配
本文出处:<Thinking in JAVA> 寄存器这是最快的存储区,因为它位于不同于其他存储区的地方--处理器内部.但是寄存器的数量极其有限,所以寄存器根据需求进行分配.你不能直接控制 ...
- 100G/40G/25G/10G网络测试解决方案
一.100G概述 随着CDN等视频直播业务和P2P业务的快速发展,带宽的要求越来越高.当前5G业务势头正盛,其基于400G的主干网络通信业务也在积极部署之中.但当前在很多的业务场景中,100G系统的部 ...
- 华为RH2288H服务器引导ServiceCD安装Windows Server操作系统
安装准备 ServiceCD光盘. Windows操作系统安装光盘. 物理光驱. 使用虚拟控制台远程安装操作系统时,需要准备以下软件: ServiceCD光盘或ServiceCD ISO文件. Win ...
- Zookeeper应用场景和ZAB协议
Zookeeper应用场景 数据发布/订阅(配置中心) 我们平常的开发过程中,经常会碰到这样的需求:系统中需要一些通用的配置信息,如一些运行时的开关.前端需要展示的通知信息.数据库配置信息等等.这些需 ...
- Oracle的用户权限和角色
用户和权限 LOCK|UNLOCK创建用户时是否锁定,默认为锁定状态.锁定的用户无法正常的登录进行数据库操作. --给普通用户SCOTT解锁,同时把SCOTT用户的密码进行修改 --语法结构:ALTE ...