-3

Write a function that takes an area code, a three digit number,and produces a seven digit phone number

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

What is display after the code is run is the following:

What 3 digits area code do you want in your number?: 240

Your new phone number is 240 1 6 3 2 1 9 1

But is there a way that I can code this better or another way? Like having the the number come out like this: (240) 163-2191

Community
  • 1
  • 1
  • 6
    Sounds kinda like a homework question... – dgel Dec 11 '14 at 04:18
  • No, I'm learning python on my own. And today while I was reading the book I got the idea to write a program that ask for a 3 digit area code, and then returns a 10 digit number lol... I'm getting close, but I think there is a better way to do that. – wallstreet809 Dec 11 '14 at 04:25
  • [What's the best way to format a phone number in Python?](http://stackoverflow.com/q/7058120) – jscs Dec 11 '14 at 04:25

1 Answers1

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
John La Rooy
  • 295,403
  • 53
  • 369
  • 502