Python中存放10000个6位随机数字的验证码

练习:创建一个文件,名字为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()

猜你喜欢

转载自blog.csdn.net/qq_39138295/article/details/80904775