python_生成隨機手機號
阿新 • • 發佈:2018-12-01
寫個一函式,這個函式的功能是,傳入一個數字,產生N條手機號,產生的手機號不能重複。
[150,189,188,170,132,150,186]
def phone(500):
phone.txt
1861232323
23423423
import random
import string
#寫一個檔案
def phone_num(num):
phone_nums=set()#集合不重複,無序
num_start = ['131','132','133','150','186','189','177']
for i in range (num):
num_starts =random.choice(num_start)
num_end =''.join(random.sample(string.digits,8))
res = num_starts+num_end+'\n'
# print("num_start is {} \t num_end is {}".format(num_starts,num_end))
#將生成的手機號新增到集合
phone_nums.add(res)
with open('phone_nums.txt','w',encoding='utf-8') as fw:
fw.writelines(phone_nums)
phone_num(10)