用於生成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”就可以了