溫馨提示×

python怎么實現(xiàn)隨機生成六位驗證碼

小億
227
2024-01-18 18:30:02
欄目: 編程語言

可以使用random模塊中的randrange函數(shù)來生成隨機的六位驗證碼。以下是一個示例代碼:

import random

def generate_verification_code():
    code = ""
    for _ in range(6):
        digit = random.randrange(0, 10)
        code += str(digit)
    return code

verification_code = generate_verification_code()
print(verification_code)

在上述代碼中,生成驗證碼的函數(shù)generate_verification_code通過循環(huán)隨機生成六個0到9之間的數(shù)字,并將其轉(zhuǎn)換為字符串,最后將生成的六個數(shù)字拼接在一起作為驗證碼。運行代碼后,會輸出一個隨機生成的六位驗證碼。

0