112k views
3 votes
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,R and S=7 T,U, and V=8 W,X,Y, and Z=9 Write a function named convert() that takes any string of alphanumeric characters and returns a string with all alphabetic characters in the argument converted into their numeric equivalent, using the above mapping. Many companies use telephone numbers like 713-FIX-CARS to make it easier for their customers to remember. However, for customers, it is easier to dial a pure numeric phone number, rather than an alphanumeric number. Write a Python program that asks the user to enter a 10-character alphanumeric phone number into its equivalent numeric phone number and display the result. For example, if the user enters 713- FIX-CARS the application should display 713-349-2277. The application should allow the user to continue translating phone numbers until she/he decides to stop. Use meaningful variable names. Prepend a preface. Include the algorithm in the form of Pseudocode by means of comment lines at the top of your Python program. Draw and submit a Flowchart for your program.

1 Answer

4 votes

A python program that asks the user to enter a 10-character alphanumeric phone number into its equivalent numeric phone number is shown.

How to write a Python Program?

A python program that asks the user to enter a 10-character alphanumeric phone number into its equivalent numeric phone number

# dict for letter in the phone is:

phone_letters = {'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'}

number = input('Enter a 10-character telephone number in the format XXX-XXX-XXXX:').replace('-', '')

answer = []

for n in number:

if n.isdigit():

answer.append(n)

if n.isalpha():

value = phone_letters.get(n.upper())

answer.append(value)

# convert list to desired format

answer.insert(3, '-')

answer.insert(7, '-')

# join list in string

print('Your phone number:', ''.join(answer))

INPUT:

Enter a 10-character telephone number in the format XXX-XXX-XXXX:555-GET-FOOD

OUTPUT:

Your phone number: 555-438-3663

User EndlessSpace
by
7.3k points