python应用-输出验证码

  

from random import randint
def generate_code (code_len):
    """
    生成确定位数的验证码
    :param code_len: 验证码长度
    :return: 由大小写英文字母和数字构成的随机验证码
    """

    all_chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    last_pos=len(all_chars)-1
    code=''
    for _ in range(code_len):
        index=randint(0,last_pos)
        code+=all_chars[index]
    return  code

if __name__ == '__main__':
    print(generate_code(4))

  结果: WL9B

原文地址:https://www.cnblogs.com/68xi/p/8546418.html