In: Computer Science
In PYTHON
On a standard telephone, 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, and R = 7
T, U, and V = 8
W, X, Y, and Z = 9
Write a fruitful function that takes any string of alphanumeric characters and returns a string with all alphabetic characters in the argument translated into their numeric equivalent, using the above mapping.
Alphabetic Telephone Number Translator: Many companies use telephone numbers like 713-GET-FOOD to make it easier for their customers to remember. But for customers, it is easier to dial a pure numeric phone number, rather than an alphanumeric number. Write an application program that asks the user to enter a 10 character alphanumeric telephone number in the form of XXX-XXX-XXXX. The application should then call your function to translate the alphanumeric phone number into its equivalent numeric phone number and display the result. For example if the user enters 713-GET-FOOD the application should display 713-438-3663. The application should allow the user to continue translating phone numbers until she/he decides to stop. Use meaningful variable names.
Python Program:
def telephone_number_translator():
userInput = True
while userInput:
# phoneNumber => For company use
phoneNumber = input('Enter the number in the format of XXX-XXX-XXXX eg. {713-GET-FOOD}: ')
newPhoneNumber = '' # For customer use
for char in phoneNumber: # for each character in input phone number
# Mapping the character by using if-else except "-"
if char == 'A' or char == 'B' or char == 'C':
char = '2'
elif char == 'D' or char == 'E' or char == 'F':
char = '3'
elif char == 'G' or char == 'H' or char == 'I':
char = '4'
elif char == 'J' or char == 'K' or char == 'L':
char = '5'
elif char == 'M' or char == 'N' or char == 'O':
char = '6'
elif char == 'P' or char == 'Q' or char == 'R' or char == 'S':
char = '7'
elif char == 'T' or char == 'U' or char == 'V':
char = '8'
elif char == 'W' or char == 'X' or char == 'Y' or char == 'Z':
char = '9'
newPhoneNumber = newPhoneNumber + char # for "-"
print("Customer New Telephone Number : ", newPhoneNumber)
choice = input("\nDo you want to convert another Telephone Number (Yes/No) ? : ")
if (choice.lower() == "yes"):
userInput = True
elif (choice.lower() == "no"):
print("Thank you !!")
break
if __name__ == '__main__':
print("********************Welcome To Alphabetic Telephone Number Convertor***********************\n")
telephone_number_translator()
Output:
Thumbs Up Please !!!