做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?
保存到mysql里面
import random import pymysql #操作数据库 pymysql class MYsql(): def __init__(self): self.CONNECTION = pymysql.connect( host = "127.0.0.1", port = 3306, user = "admin", password = "admin", db = "weather", charset = "utf8", cursorclass=pymysql.cursors.DictCursor ) def writesql(self,code_key): SQL = "INSERT INTO code(name) VALUES ('%s')" % code_key #sql语句 存放激活码进入mysql try: with self.CONNECTION.cursor() as cursor: cursor.execute(SQL) self.CONNECTION.commit() except Exception as e: print(repr(e)) #生成200个激活码 激活码的格式统一 可用随机数 验证码加长 #大写字母ascll (65-90) 小写字母ascll(97-122) #比如设置12位激活码 #定义激活码函数 def getCode(): code = ''#先生成一个空字符串 for i in range(13): first = chr(random.randint(65,90)) second = str(random.randint(0,9)) three = chr(random.randint(97,122)) add = random.choice([first,second,three]) code = "".join([code,str(add)]) return code # print(CODE) #将激活码写进mysql if __name__ == '__main__': codekey = MYsql() alm = int(input("需要生成多少个激活码:")) for i in range(alm): code_key = getCode() #生成验证码 codekey.writesql(code_key) #写入数据库 print('恭喜你,写入成功!')