首页 > 移动平台 > 详细

day6_随机生成11位手机号的程序

时间:2018-03-08 13:24:44      阅读:223      评论:0      收藏:0      [点我收藏+]
#需求分析:
#1、前三位确定,可以定义一个list,如lis = [‘131‘, ‘132‘, ‘133‘, ‘134‘, ‘135‘,‘136‘,‘137‘,‘138‘,‘139‘,‘158‘, ‘159‘, ‘185‘, ‘186‘, ‘188‘]
#2、后面的八位随机取,通过random.sample(str,8)

用list添加手机号:
def phone_num(num):
import random,string
all_nums = []
for i in range(num):
lis = [‘131‘, ‘132‘, ‘133‘, ‘134‘, ‘135‘, ‘158‘, ‘159‘, ‘185‘, ‘186‘, ‘188‘]
start = random.choice(lis)
end = ‘‘.join(random.sample(string.digits, 8))
res = start + end + ‘\n‘
if res not in all_nums:
all_nums.append(res)
with open(‘telephone.txt‘, ‘w‘, encoding=‘utf8‘) as fw:
fw.writelines(all_nums)
phone_num(10)

用集合添加手机号:
def phone_num(num):
    import random,string
all_phone_nums = set()
num_start = [‘134‘, ‘135‘, ‘136‘, ‘137‘, ‘138‘, ‘139‘, ‘150‘, ‘151‘, ‘152‘, ‘158‘, ‘159‘, ‘157‘]
for i in range(num):
start = random.choice(num_start)
end = ‘‘.join(random.sample(string.digits,8))
res = start + end + ‘\n‘
all_phone_nums.add(res)
with open(‘phone.txt‘,‘w‘,encoding = ‘utf8‘) as fw:
fw.writelines(all_phone_nums)
phone_num(20)
 

day6_随机生成11位手机号的程序

原文:https://www.cnblogs.com/laosun0204/p/8527544.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!