1. 程式人生 > >Python隨機生成一個六位數的驗證碼

Python隨機生成一個六位數的驗證碼

import random
str = ""
str1 = ""
list = []
index = 0
num = 0
while num < 6:
    x = random.choice(range(12))   # 對隨機生成的字元進行隨機排序
    if x < 4:
        list.append(chr(random.choice(range(10)) + 48))   #隨機生成一個字元0—9
    elif x < 8:
        list.append(chr(random.choice(range(65, 90))))    #隨機生成一個字元A—Z
    else:
        list.append(random.choice(chr(random.choice(range(97, 122))))) #隨機生成一個字元a—z
    num += 1
while index < 6:
    str1 = list[index]
    str += str1
    index += 1
print(str)