nbsp ice range randint bin return sage 驗證碼 span

實現一個簡單的驗證碼生成器

#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "loki"

# Usage: 驗證碼生成

import random


def verification_code(digit):
    verify_code = ""
    for count in range(0, digit):
        int_num = random.randint(0, 9)
        lower_case = chr(random.randint(97, 122))
        upper_case = chr(random.randint(65, 90))
        compose = random.choice([int_num, lower_case, upper_case])
        verify_code += str(compose)
    return verify_code


print(verification_code(5))  # 5位數的驗證碼
print(verification_code(10))  # 10位數的驗證碼

python模塊——random模塊(簡單驗證碼實現)