1. 程式人生 > >python隨機產生手機號和郵箱號

python隨機產生手機號和郵箱號

1、寫個一函式,這個函式的功能是,傳入一個數字,產生N條手機號,產生的手機號不能重複。
[150,189,188,170,132,150,186]
def phone(500):
phone.txt
2、寫一個函式,這個函式的功能是,傳入一個數字,產生N條郵箱,產生的手機號不能重複。
郵箱前面的長度是6-12之間,產生的郵箱必須包含大寫字母、小寫字母、數字和特殊字元
[163.com,qq.com,sina.com,126.com]

1、

def random_PhoneNumber(N):
import random
l = []
for i in range(N):
s=[150,189,188,170,132,150,186]
h=random.choice(s)
eight=random.randint(10000000,99999999)
p="{}{}".format(h,eight)
if p not in l:
l.append(p)
else:
i -= 1
m=set(l)
phone = '\n'.join(l)
with open('phone.txt', 'w+') as f:
f.writelines(phone)
return len(m)
print(random_PhoneNumber(500))

2、

def random_email(N):
import string
import random
m = []
l = []
l.append(string.digits)
l.append(string.ascii_letters)
l.append(string.punctuation)
l=''.join(l)
i=0
while i < N:
s = ['@163.com','@qq.com','@sina.com','@126.com']
h = random.choice(s)
rang = random.randint(6,12)
randomNumber = "".join(random.choice(l) for j in range(rang))
email = set(randomNumber)
lowletters = set(string.ascii_lowercase)
upperletters = set(string.ascii_uppercase)
digits = set(string.digits)
punctuation = set(string.punctuation)
if email&lowletters and email&digits and email&punctuation and email&upperletters:
p="{}{}".format(randomNumber,h)
if p not in m:
m.append(p)
i += 1
n=set(m)
phone = '\n'.join(m)
with open('email.txt', 'w+') as f:
f.writelines(phone)
return len(n)
print(random_email(500))