python之隨機生成手機號



import random
'''
手機號的規則如下
第一位:1;
第二位:3,4,5,7,8
第三位:
3:0-9
4:5,6,7
5:0,1,2,3,5,6,7,8,9
7:6,7,8
8:0-9
'''
def telphone():
  second = random.choice([3,4,5,7,8])#第二位值,從此列表隨機生成

  third = {
  3:random.randint(0,9),
  4:random.choice([5,7]),
  5:random.choice([0,1,2,3,5,6,7,8,9]),
  7:random.choice([6,7,8]),
  8:random.randint(0,9)
  }[second]#根據second的值,來生成第3位的值

  behind = ''#定義個空字符串
  for i in range(8):
  behind = behind + str(random.randint(0,9))#8位數字中的每一位從0-9中生成,8次循環后,字符串相加成為8位數
  phone_number = str(1) + str(second) + str(third) + behind#四組字符相加,生成手機號
  print(phone_number)
  return phone_number

for i in range(10):
telphone()#調用生成手機號函數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM