十位隨機驗證碼

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

練習:

建立一個檔案,在裡面存放10000個十位隨機驗證碼

 

import random

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

for x in range(100000):
    
    str = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789'

    list = ''
    
    for y in range(10):
       
        a = random.choice(str)
        
        list += a
    
    f.write(list+'\n')

f.close()

 

獲取上述中的檔案,然後判斷每個驗證碼中數字出現的次數。

 

f = open('code2.txt','r',encoding='utf-8')

a=f.readlines()

for x in a:
    
    i = 0
    
    for y in x.strip():
      
        if y.isdigit():
         
            i+=1
        
        else:
           
            pass
   
    f1 = open('code3.txt', 'a', encoding='utf-8')
    
    f1.writelines(str(i)+'\n')
    
    f1.close()

f.close()

統計所有的數字出的次數:

 

f = open('code2.txt','r',encoding='utf-8')

num_count = {}

a = f.read()

num_count['0'] = a.count('0')

num_count['1'] = a.count('1')

num_count['2'] = a.count('2')

num_count['3'] = a.count('3')

num_count['4'] = a.count('4')

num_count['5'] = a.count('5')

num_count['6'] = a.count('6')

num_count['7'] = a.count('7')

num_count['8'] = a.count('8')

num_count['9'] = a.count('9')

print(num_count)

執行結果如下:

{'0': 15978, '1': 16399, '2': 16539, '3': 16163, '4': 16269, '5': 16102, '6': 16275, '7': 15937, '8': 15934, '9': 16001}

 

 

 

 

 

 

相關文章