python第二題 學習random的用法

來源:互聯網
上載者:User

標籤:div   知識   range   有序   import   使用   啟用   body   函數   

做為 Apple Store App 獨立開發人員,你要搞限時促銷,為你的應用產生啟用碼(或者優惠券),使用 Python 如何產生 200 個啟用碼(或者優惠券)


import string,random
poolOfChars = string.ascii_letters + string.digits
#ascii_letters是產生所有字母,從a-z和A-Z,digits是產生所有數字0-9.
random_codes = lambda x, y: ‘‘.join([random.choice(x) for i in range(y)])
for i in range(200):
print(random_codes(poolOfChars, 15))

random 庫中的 randint(inf, sup) 函數可以在指定的範圍內產生一個隨機整數;

choice(sequence) 可以在一個有序的類型中(比如 listtuple 或 string)隨機選取一個元素。

ascii_letters是產生所有字母,從a-z和A-Z,digits是產生所有數字0-9.
那啟用碼就是亂序的數字和字母組合,每次從string.ascii_letters + string.digits是全部的數字和字母組合,那隨機選取一個,在用字串的join連結起來,連結指定位元就行了
lambda 是一個知識點,在廖雪峰的網站上也有學習。lambda就是一個匿名函數  可以使代碼更加簡潔。
可參考:http://blog.csdn.net/yezonggang/article/details/50978114

關於string的學習 參考 https://www.cnblogs.com/rollenholt/archive/2011/11/25/2263722.html

 

python第二題 學習random的用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.