1. 程式人生 > >Python學習之路:random模塊

Python學習之路:random模塊

和數 highlight body ima bubuko randint 之路 ren 分享

技術分享圖片

技術分享圖片

#隨機生成4位數字的驗證碼
# import random
#
# checkcode=‘‘
#
# for i in range(4):
#     current=random.randint(1,9)
#     checkcode+=str(current)
# print(checkcode)

#隨機生成4位字母和數字組合的驗證碼
import random
checkcode=‘‘
for i in range(4):
    current=random.randrange(0,4)
    if current==i:
        tmp=chr(random.randint(65,90))
    else:
        tmp=random.randint(0,9)
    checkcode+=str(tmp)
print(checkcode)

Python學習之路:random模塊