zoukankan      html  css  js  c++  java
  • 【Python&练习题】生成随机激活码

    题目来源:https://github.com/Yixiaohan/show-me-the-code

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

    ①激活码位数可控,一般是12或者16位,控制最大位数为16位;

    ②一次性生成的激活码的个数可控;

    ③激活码包括大写字母、数字;

    ④激活码不能重复;

     1 import random,string#导入模块
     2 
     3 a = string.digits +string.ascii_letters
     4 #string.digits>>>数字_字符串0123456789
     5 #string.ascii_letters>>>字母_字符串,大小写所有字母
     6 #a=代表生成随机字符串的库
     7 
     8 def get_key(Activation_Code_length):
     9     key = ''
    10     for i in range(1,Activation_Code_length+1):
    11         key += random.choice(a) #获取随机字符或数字
    12         if i % 4 == 0 and i !=Activation_Code_length: #每隔4个字符增加'-'
    13             key += '-'
    14     print(key)
    15     return key
    16 
    17 def get_all_keys():
    18     tmp=[]
    19     for i in range(Activation_Code_Number):
    20         one_key = get_key(Activation_Code_length)
    21         if one_key not in tmp: #去掉重复key
    22             tmp.append(one_key)
    23     for key in tmp:
    24         with open('激活码.txt','a+') as f:
    25             f.write(key+'
    ') #写入激活码
    26 
    27 Activation_Code_length = int(input('输入激活码长度:'))
    28 Activation_Code_Number = int(input('输入要生成的激活码数量,输入0退出:'))
    29 get_all_keys()
  • 相关阅读:
    VS2013快速安装教程
    软件工程课程的感想
    GitHub和Microsoft TFS对比有什么优势
    拼接素数
    C语言程序题
    vue中的实例方法的底层原理
    ios 安卓
    防抖
    伪数组转真数组的放法
    http和https的一种能力?
  • 原文地址:https://www.cnblogs.com/jason-syc/p/11202901.html
Copyright © 2011-2022 走看看