Python - random庫使用例子——生成隨機驗證碼


用於生成4位隨機驗證碼

#_*_coding:utf-8_*_
#__author__ = "csy"
import random
checkcode=''
for i in range(4):
    current=random.randrange(0,4)   #生成隨機數與循環次數比對
    current1 = random.randrange(0,4)
    if current == i:
        tmp=chr(random.randint(65,90))   #65~90為ASCii碼表A~Z
    elif current1 == i:
        tmp = chr(random.randint(97,122))   #97~122為ASCii碼表a~z
    else:
        tmp=random.randint(0,9)
    checkcode+=str(tmp)
print(checkcode)

 注意該python文件名為“random”,運行時可能會出現 AttributeError: module 'random' has no attribute 'randrange'錯誤提示,后來文件名改為“random1”就可以了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM