0%

Python练习册:0001

========================

pacin npm --overwrite /usr/lib/node_modules/npm/node_modules/yargs/node_modules/strip-ansi/* --overwrite /usr/lib/node_modules/npm/node_modules/yargs/node_modules/string-width/ --overwrite

题目

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

分析

激活码,一般是随机生成的一串字符,需要确保唯一性。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import secrets

def Generate_Key(counts,length=20):
key_list = []

for i in range(counts):
key = str(secrets.token_hex(length))[:length]
if key not in key_list:
#五个字符一组,添加-作为连字符
for j in range(5,length,6):
key = key[:j] + '-' + key[j:]

key_list.append(key)

return key_list

print(Generate_Key(200,20))
1
2
3
4
5
6
7
8
9
10
11
12
13
14

import uuid

def Generate_Code(counts,length=20):
key_list = []

for i in range(counts):
key = str(uuid.uuid4())[:length]
if key not in key_list:
key_list.append(key)

return key_list

print(Generate_Code(200,20))

参考

欢迎关注我的其它发布渠道