Python 練習冊,每天一個小程序,原題來自Yixiaohan/show-me-the-code
我的代碼倉庫在Github
目標
做為 Apple Store App 獨立開發者,你要搞限時促銷,為你的應用生成激活碼(或者優惠券),使用 Python 如何生成 200 個激活碼(或者優惠券)?
解決方案
該題目采用 python 中的 uuid 來實現,代碼如下:
import uuid
# 生成 num 個驗證碼,每個長度為length,可設置默認長度
def create_num(num, length=16):
result = []
while num > 0:
uuid_id = uuid.uuid1()
# 刪去字符串中的'-',取出前length 個字符
temp = str(uuid_id).replace('-', '')[:length]
if temp not in result:
result.append(temp)
num -= 1
return result
print(create_num(200, 32))
print(create_num(200))