Write a program that asks the user to enter a 10 character telephone number in the format XXX-XXX-XXXX. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD the application should display 555-438-3663.



def main():
#character string
numeric_phone=" "

#user inputs phone number
alpha_phone = input('Enter 10-Digit phone number: ')

digit_list = ["2", "3","4", "5", "6", "7", "8", "9", "0"]



for ch in alpha_phone:
if ch.isalpha():
ch = ch is upper()

Respuesta :

Answer:

import re  

dict = {

   "A"  :"2", "B"  : "2", "C"  : "2",

   "D": "3", "E"  : "3", "F" : "3",  

   "G": "4", "H"  : "4", "I" : "4",

   "J": "5", "K"  : "5", "L" : "5",

   "M" : "6", "N"  : "6", "O"  : "6",

   "P": "7", "Q"  : "7", "R" : "7", "S" : "7",

   "T": "8", "U"  : "8", "V" : "8",

   "W": "9", "X"  : "9", "Y": "9", "Z" : "9",

 }  

def phoneAlphaConverter(dict, text):

 # Create a regular expression  from the dictionary keys

 regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))

 

 # For each match, look-up corresponding value in dictionary

 return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)  

alpha_phone = input('Enter 10-Digit phone number: ').upper()

print(phoneAlphaConverter(dict, alpha_phone))

Explanation:

The programming language used is python 3.

The numeric equivalent of alphabets are as follows:

  • 2 ABC
  • 3 DEF
  • 4 GHI
  • 5 JKL
  • 6 MNO
  • 7 PQRS  
  • 8 TUV
  • 9 WXYZ

In the code, It begins by importing the regular expression module which will allow us carry out special string operations on text.

Next, a dictionary is defined to hold all the alphabets and their number equivalent.

A function that would carry out the replacement of alphabets with their numbers is created; In the function, a regular expression is created with the dictionary keys (Alphabet), and for each dictionary key, the function will substitute it with its value (Number) and return it.

Finally, the code prompts the user to enter a 10-digit number, converts all the alphabets in the input to upper case using the .upper() method, The code calls the function and prints the result.

check attachment to see code in action.