第 0001 题: 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?
解答:假设生成的激活码为16位code,如:Qlgh-2zEx-dmGg-vzWQ,每个code由数字、大小写字母组成;
完成这个题,需要用Python中两个库:
(1)random.randint(a,b)随机生成一个范围a-b之间的整数
(2)string库:String
模块中,包含大量实用常量和类:
string.ascii_lowercase
包含所有小写字母的字符串常量:'a-z'
string.ascii_uppercase
包含所有大写字母的字符串常量:'A-Z'
string.ascii_letters
包含所有大小写字母的字符串常量:'a-zA-Z'
string.digits
包含数字0到9的字符串常量:'0123456789'
附上代码:
import random
import string
s = string.ascii_letters + string.digits
print(s)
code_list=[]
for i in range(0,200):
code = ""
for j in range(0, 16):
if j % 4 == 0 and j:
code += "-"
code += s[random.randint(0, len(s)-1)]
code_list.append(code)
print(code_list)
每隔四个字符,添加一个“-”,运行结果为:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
['Q5xC-svtC-UPiL-cdds', 'Ix1H-pDsv-ylys-OQ3n', 'pI1M-IoB1-Kff1-2VHf', 'msKn-SrY1-4GY3-gAET', 'cg5J-DpyF-zFgg-rHqZ', 'kn0s-1mam-ZgCo-9N5c', 'nheB-f06e-1sp6-CDr5', 'DO2L-6Qne-SlPY-JtfM', 'BjUF-If9T-fKaA-8wGX', 'VbpJ-e8Q7-avGR-rijd', 'r7Jq-QYuA-2UPh-FFmj', 'ODEm-60YT-NBJM-dl2g', '0cvY-Tiso-PqFn-3G84', 'tBjU-FSlW-rc5A-6upE', 'ZajQ-p7Jd-kZO6-mR6k', 'uqNi-trYN-7xGy-DIUl', '2AVD-eLsE-blWU-zkvK', '32gJ-vj1g-CViM-ptg0', 'LGJV-93qQ-5SIu-vE7Z', '8gYO-SJts-qKKP-JQlY', 'esfP-ZrtP-89Ha-hyHb', 'di91-qIjC-GON6-zFmH', 'tAAZ-xYv2-eoQ3-jCiI', 'RRdc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