如何生成更多字符?

Mad*_*pop 2 python

我正在根据流行的美国游戏制作代码生成器,它有 4 个字母的游戏代码。我构建了这个程序来为你生成一个,看看它是否真的存在,但不是四个字母,而是一个被生成四次。

我怎样才能解决这个问题?提前致谢!

import random
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
quantity = int(input('How many codes do you want? '))

def create_code():
     for c in range(4):
         code = ''
         code += random.choice(chars)

         return code

for i in range(0, quantity):
    print('Your code is', create_code())
    
Run Code Online (Sandbox Code Playgroud)

jsm*_*art 6

这是另一种方式,它使用.choices()(复数)

import random
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
list_of_chars = random.choices(chars, k=4)
print(''.join(list_of_chars))
Run Code Online (Sandbox Code Playgroud)