编写一个功能,该功能采用区号,三位数字,并产生七位数的电话号码
import random
def area_code(r):
print "Your new phone number is (%s)" % r,
i = 1
while i <= 7:
#It assigned a random number from 0 to 9 to the variable n.
n = random.randint(0, 9)
print n,
i += 1
运行代码后显示的内容如下:
您想要的3位数区号码是什么?:240
您的新电话号码是240 1 6 3 2 1 9 1
但有没有办法可以更好地或其他方式编码? 就像这个数字出来一样:(240)163-2191
答案 0 :(得分:1)
>>> from random import randrange
>>> area_code = "240"
>>> print "Your new phone number is ({}) {}{}{}-{}{}{}{}".format(area_code, *[randrange(10) for i in range(7)])
Your new phone number is (240) 570-2745