Python中存放10000個6位隨機數字的驗證碼

回憶不說話發表於2018-07-03

練習:建立一個檔案,名字為code.txt,在裡面存放10000個6位隨機數字的驗證碼。

方法一:

import random

f = open('code.txt','w',encoding='utf-8')

for x in range(10000):

   num = random.randint(0,999999)

   num = '%.6d'%num

   f.write(num+'\n')

f.close()

 

方法二:

 


 
import random

f = open('code.txt','w',encoding='utf-8')

num = 0

while(num<10000):

    l = []

    for x in range(6):

        rand_num = random.randint(0,9)

        l.append(str(rand_num))

    for i in l :

        f.write(i)

        num+=1

    f.write('\n')

f.close()

方法三:

 


 
import random

f = open('code.txt','w',encoding='utf-8')

for x in range(10000):

    count = ''

    for y in range(6):

        num = random.randint(0,9)

        count += str(num)

    f.write(count+'\n')

f.close()

 

 

 

 

 

 

 

 

 

 

相關文章