In: Computer Science
Alphabetic Telephone Number Translator
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
For your program, you will be using a secret code as show below:
A and C = 2
D and E = 3
B, G, H, and I = 4
J, K, L, and X = 5
F, M, N, O, and P = 6
Q = 7
R, S, T, U, and V = 8
X, Y, and Z = 9
Write a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX (where X can be an upper- or lower-case letter or a number).
Python Program:
""" Python Program that translates Phone number into Numerical Format """
def getCharacter(ch):
# Checking character for corresponding alphabet
if ch in "ABC":
return '2'
elif ch in "DEF":
return '3'
elif ch in "GHI":
return '4'
elif ch in "JKL":
return '5'
elif ch in "MNO":
return '6'
elif ch in "PQRS":
return '7'
elif ch in "TUV":
return '8'
elif ch in "WXYZ":
return '9'
else:
return ch
def getInput():
""" Reads input from user """
# Reading phone number from user
phoneNum = input("Enter the number in the format of XXX-XXX-XXXX:
")
return phoneNum.upper()
def main():
""" Main function """
# Reading phone number from user
phoneNum = getInput()
new_phone_num = ""
# Iterating over each character
for ch in phoneNum:
# Checking character for corresponding alphabet
new_phone_num += getCharacter(ch)
# Printing new phone number
print(new_phone_num)
# Calling main function
main()
Code Screenshot:
Sample Run: