on Phyton 3.6.2 On a standard telephone, alphabetic letters are mapped to number
ID: 3595805 • Letter: O
Question
on Phyton 3.6.2
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 1-4 J, K, and L-5 M, N, andO 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. (6 points) 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. (6 points)Explanation / Answer
alpha_dict = {}
alpha_dict['A'] = 2
alpha_dict['B'] = 2
alpha_dict['C'] = 2
alpha_dict['D'] = 3
alpha_dict['E'] = 3
alpha_dict['F'] = 3
alpha_dict['G'] = 4
alpha_dict['H'] = 4
alpha_dict['I'] = 4
alpha_dict['J'] = 5
alpha_dict['K'] = 5
alpha_dict['L'] = 5
alpha_dict['M'] = 6
alpha_dict['N'] = 6
alpha_dict['O'] = 6
alpha_dict['P'] = 7
alpha_dict['Q'] = 7
alpha_dict['R'] = 7
alpha_dict['T'] = 8
alpha_dict['U'] = 8
alpha_dict['V'] = 8
alpha_dict['W'] = 9
alpha_dict['X'] = 9
alpha_dict['Y'] = 9
alpha_dict['Z'] = 9
def numbericFromAlpha(alpha):
if alpha in alpha_dict:
return alpha_dict[alpha]
return alpha
def isNumberValid(number):
if len(number) != 12 or number[3] != "-" or number[7] != "-":
return False
indx = [0,1,2,4,5,6,8,9,10,11]
for i in indx:
if not number[i].isdigit() and not number[i] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
return False
return True
while True:
number = input("Enter a number in format XXX-XXX-XXXX: ")
number = number.rstrip()
if not isNumberValid(number):
print("Enter a valid number")
continue
convertedNumber = ""
for i in number:
convertedNumber += str(numbericFromAlpha(i))
print(convertedNumber)
choice = input("Do you want to convert more number: (y/n)? ")
if choice.lower() != "y":
break
# copy pastable link: https://paste.ee/p/pSQ67
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.