Python-编写一个可以生成注册码or优惠码的小代码片段
程序员文章站
2022-03-04 19:01:58
# 使用了random模块 - 好像有string模块可以省去 assi 编码库的编写,但是还未了解string模块 ......
1 import random 2 ''' 3 此程序生成一个5 x 5的包含阿拉伯数字和26个不区分大小写字母的激活码 4 例: 12Df7-V8Fd1-mg8Yr-s9IkJ-ds98s 5 ''' 6 class JiHuoMa(object): 7 def __init__(self, ma=None, shi_yong="used"): 8 self.__ma = JiHuoMa.random_ma() 9 self.__zhuang_tai = shi_yong 10 11 def __str__(self): 12 return self.ma 13 14 @property 15 def ma(self): 16 return self.__ma 17 # 获取某个激活码实例 18 19 def get_zt(self): 20 return self.__zhuang_tai 21 22 def set_zt(self, value): 23 if value == "yes" or "no": 24 self.__zhuang_tai = value 25 else: 26 raise ValueError("plz input yes or no!") 27 28 zt = property(fget=get_zt, fset=set_zt, fdel=None, doc=None) 29 # 获取和设置激活码是否被使用的状态 30 31 @staticmethod 32 def random_ma1(): 33 # 随机其中一组激活码 34 assi = ("a", "b", "c", "d", "e", "f", "g",\ 35 "h", "i", "j", "k", "l", "m", "n",\ 36 "o", "p", "q", "r", "s", "t", "u",\ 37 "v", "w", "x", "y", "z", "1", "2",\ 38 "3", "4", "5", "6", "7", "8", "9"\ 39 ) 40 ma_1 = [x+y+z+c+v+"-" for x in random.choice(assi) for y in random.choice(assi)\ 41 for z in random.choice(assi) for c in random.choice(assi) for v in random.choice(assi)] 42 43 return ma_1 44 45 @staticmethod 46 def random_ma(): 47 ma = JiHuoMa.random_ma1() + JiHuoMa.random_ma1() + \ 48 JiHuoMa.random_ma1() + JiHuoMa.random_ma1() + \ 49 JiHuoMa.random_ma1() 50 return ''.join(ma)[:-1].upper() 51 # 组成4x4的激活码,以“-”号分割,且去掉最后一个“-” 52 53 54 if __name__ == '__main__': 55 j1 = JiHuoMa() 56 print(j1.ma) 57 print(j1.zt)
# 使用了random模块
- 好像有string模块可以省去 assi 编码库的编写,但是还未了解string模块