Many companies use telephone numbers like 555-GET-FOOD so the number is easier for their customers to remember. On a standard telephone, the alphabetic letters are mapped to numbers in the following fashion: A, B, and C = 2 D, E, and F = 3 G, H, and I = 4 J, K, and L = 5 M, N, and O = 6 P, Q, R, and S = 7 T, U, and V = 8 W, X, Y, and Z = 9 Write a program that asks the user to enter a 12-character telephone number in the format: XXX-XXX-XXXX. Acceptable characters (X's) are A-Z and a-z. Your program should check for: The length of the phone number is correct. The dashes are included and are in the correct positions. There are no characters in the illegal characters in the string. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. If the input string is not entirely correct then you should print an error message. For example, if the user enters 555-GET-FOOD the program should display 555-438-3663. If the user enters 123-456-7890, the program should display 123-456-7890. Rules: You must have one function (in addition to main()) that converts an alphabetic character to a digit. Or you can have one function that coverts all characters to digits. You can make your own function or use a built-in function if one exists.

Respuesta :

Answer:

The program in Python is as follows:

def convertt(phone):

splitnum = phone.split ('-')

valid = True  

count = 0  

err = ""  

numphone = ""

if len(phone) != 12:

 err = "Invalid Length"  

 valid = False  

elif phone[3] != '-' or phone[7] != '-':

 err = "Invalid dash [-] location"  

 valid = False  

while valid== True and count < 3:

 for ch in splitnum[count]:

  if ch.isdigit():

   numphone += ch  

  elif ch.upper()in 'ABC':

   numphone += '2'  

  elif ch.upper()in 'DEF':

   numphone += '3'  

  elif ch.upper()in 'GHI':

   numphone += '4'  

  elif ch.upper() in 'JKL':

   numphone += '5'  

  elif ch.upper()in 'MNO':

   numphone += '6'  

  elif ch.upper()in 'PQRS':

   numphone += '7'  

  elif ch.upper()in 'TUV':

   numphone += '8'  

  elif ch.upper()in 'WXYZ':

   numphone += '9'

  else:

   valid = False

   err = "Illegal character in phone number"  

 if count!=2:

  numphone += '-'  

 count += 1  

if valid == False:

 print (err)

else:

 print ("Phone Number", numphone)

phone = input("Phone number: ")

convertt(phone)

Explanation:

See attachment for complete source code where comments are used for explanation

Ver imagen MrRoyal
ACCESS MORE
EDU ACCESS
Universidad de Mexico