写个一函数,这个函数的功能是,传入一个数字,产生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+' '
# print("num_start is {} 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)