Python: Write a function called “phoneNumberFormat” that takes a string containi
ID: 3690242 • Letter: P
Question
Python: Write a function called “phoneNumberFormat” that takes a string containing a ten-digit phone number (such as 5155551212) as input, convert into a more readable string with parentheses and dashes like (515)555-1212 and display it. If the input string contains more than or less than ten characters, display an error message. Note: Steps of phoneNumberFormat function can be given as follows: def phoneNumberFormat (phoneNum): 1. Let l be the length of the phoneNum 2. If l not equals 10 then, display an error message. 3. Otherwise, i. areaCode = first three characters of phobeNum. ii. next3digits = the substring of the next three digits of phoneNum iii. last4digits = the substring of the last four digits of phoneNum iv. Concatenate the substrings in the order, open parenthesis, areaCode, close parenthesis, next3digits, dash, last4digits and display the result. For example, phoneNumberFormat (“5155551212”) displays the string “(515)555- 1212” and switcheroo(“515555”) displays an error message. Save the script as “lab4_checkpoint1.py” in cs104lab4 folder.Explanation / Answer
def phoneNumberFormat (phoneNum):
l = len(phoneNum)
if l != 10 :
print "Not a 10 digit phone number. Thank you"
return
areaCode = phoneNum[0:3]
next3digits = phoneNum[3:6]
last4digits = phoneNum[6:]
result = "("+areaCode+")"+next3digits+"-"+last4digits
print result
return
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.