python random

random生成隨機六位驗證碼,要求大小寫字母數字都可,
能夠用來寫個雙色球,驗證碼,抽獎等東西
記住幾個常見的函數便可:
'''
import random
print(random.randint(1,10)) #返回1-10中的任意一個數字
print(random.randrange(1,3))#返回1-2中的任意一個數字
print(random.sample(range(33),6))#返回一個可迭代對象的指定幾位,如6位python

'''git

生成隨機dom

'''
checkcode = ''
for i in range(6):
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)ide

'''函數

print(''.join(random.sample((string.ascii_letters+string.digits),6))) #直接這樣寫也能夠code

犯了一個錯誤就是個人py名字寫成了random.py,致使我寫完上邊第一個幾個經常使用的函數,而後執行發現沒有這個方法,後來才發現搞錯了,文件名跟函數名搞一致了,後來改爲了randomd.py就沒事了對象

另外這個能夠看你執行的random是哪兒的東西,網上好多人估計也有這個問題,blog

python random

相關文章
相關標籤/搜索