隨機產生六位驗證碼函數版(python)

來源:互聯網
上載者:User

標籤:隨機數   div   col   lower   ret   代碼   python   str   int   

import randomdef code(n=6,alpha=True):    s = ‘‘ # 建立字串變數,儲存產生的驗證碼    for i in range(n):  # 通過for迴圈控制驗證碼位元        num = random.randint(0,9)  # 產生隨機數字0-9        if alpha: # 需要字母驗證碼,不用傳參,如果不需要字母的,關鍵字alpha=False            upper_alpha = chr(random.randint(65,90))            lower_alpha = chr(random.randint(97,122))            num = random.choice([num,upper_alpha,lower_alpha])        s = s + str(num)    return sprint(code(6,False))  # 列印6位元字驗證碼print(code(6,True))   # 列印6位元字字母混合驗證碼print(code(4,False))  # 列印4位元字驗證碼print(code(4,True))   # 列印4位元字字母混合驗證碼

以上代碼僅供參考,有問題可以留言,相互交流!

隨機產生六位驗證碼函數版(python)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.