我正在为学校练习制作一个串行密钥生成器,但遇到了一个小问题。当我运行程序并输入我想制作 20 个串行密钥时,程序将制作 20 个相同的密钥。造成这种情况的原因是 for 循环每次经过时都会生成变量。(v{0} 的事情)
我不知道如何使用另一种方法来读取列表中的所有值,并从中创建变量。
我的代码:
import random, sys
class Application:
def __init__(self):
global i
i = int(input("How many serial codes do you want to create?\n"))
print("")
self.main(i)
def main(self, i):
seq = "ABCDFGHJIKLMNOPQRSTUVWXYZ1234567890"
store = []
for z in range(0, i):
for x in range(0, 5):
first = random.choice(seq)
second= random.choice(seq)
third = random.choice(seq)
fourth =random.choice(seq)
fifth = random.choice(seq)
serial = first + second + third + fourth + fifth
store.append(serial)
for y, item in enumerate(store):
setattr(sys.modules[__name__], 'v{0}'.format(y), item)
create = v0 + "-" + v1 + "-" + v2 + "-" + v3 + "-" + v4
print(create)
print("\nCreated", i, "serial keys!")
if __name__ == '__main__':
app = Application()