这样的随机代码列表:python上的(XXXXX-XXXXX-XXXXX)

时间:2018-06-08 01:14:11

标签: python string random

你好我想生成随机string.ascii_uppercase + string.digits的列表和我想要的代码如下:XXXXX-XXXXX-XXXXX(带有 - 的15个字符)和生成器询问你想要多少代码生成。谢谢你< 3

1 个答案:

答案 0 :(得分:-1)

from random import choice
from string import ascii_uppercase, digits

CHAR_SET = ascii_uppercase + digits


def get_integer(msg):
    while True:
        try:
            return int(input(msg))
        except ValueError:
            pass


def get_random_code(chunks=3, delim='-'):
    def get_random_str(length=5):
        return ''.join(choice(CHAR_SET) for _ in range(length))
    return delim.join(get_random_str() for _ in range(chunks))


if __name__ == '__main__':
    total_combos = get_integer('Enter # of combos to generate: ')
    for _ in range(total_combos):
        print(get_random_code())