0

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

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.

This is my work:

phoneNum = input('Enter the number in the format of 555-XXX-XXXX\n')

phoneNum = phoneNum.split('-')

for var in phoneNum[1:2]:
    for char in var:
        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'

print(phoneNum)

Run it, you will be prompted to enter a string, say, 555-GET-FOOD. Then you will receive an error message:

NameError: name 'GET' is not defined

How can I fix this?

Blckknght
  • 100,903
  • 11
  • 120
  • 169
user2796648
  • 1
  • 1
  • 1
  • 2
  • You’re probably using Python 2, not Python 3. `input` acts like that in Python 2. Use Python 3, or change it to `raw_input`. – Ry- Oct 04 '13 at 17:57
  • 1
    As a note, there are better ways of doing this - look into using data structures like a dictionary to make this task much simpler. – Gareth Latty Oct 04 '13 at 17:59
  • 1
    After fixing that, you’ll need to take into account that changing `char` doesn’t actually affect your string. – Ry- Oct 04 '13 at 18:01
  • 1
    Not related to the `input` issue, but this approach might be easier: `import string; "555-GET-FOOD".translate(string.maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "22233344455566677778889999"));`. or some other variant thereof. – twalberg Oct 04 '13 at 18:27
  • At the very least, `char == 'A' or char == 'B' or char == 'C'` can be changed to `char in ('A', 'B', 'C')`. I didn't do `char in 'ABC'` because that could match `AB` or `BC` as well. – SethMMorton Oct 04 '13 at 18:38
  • Your latest edit has turned this into a totally different question, which the answers below don't address. Please don't do that! Instead, just ask for help with your new issue as a separate question (with perhaps a link back to this one, if you think the issues are related). I'm rolling back your edit, so the answers will make sense again. If you want to see the text of your other version (to turn it into a new question), you can find it [here](http://stackoverflow.com/revisions/19187735/4). – Blckknght Oct 18 '13 at 22:14

2 Answers2

3

You say you're using Python 3, but are you sure? If you're using Python 2 then you have to use raw_input instead of input. In Python 2, input tries to eval the resulting string, so it's trying to do 555 minus a variable called GET minus a variable called FOOD.

Claudiu
  • 224,032
  • 165
  • 485
  • 680
0

In your code, char is just a copy of a character in the variable phoneNum. When you change char, you do not change phoneNum. One way to deal with this problem is to create a new variable, newPhoneNumber:

phoneNum = input('Enter the number in the format of 555-XXX-XXXX\n')
print(phoneNum)
newPhoneNumber = ''
for char in phoneNum:
    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

print(newPhoneNumber)

Discussion

  • I assume that this is a homework problem, therefore I avoid using advanced constructs such as list comprehension, or set.
  • A word about naming convention. Most Python scripts uses lower case and underscore, such as phone_number--this is not a requirement, but many people follow this convention.
  • Furthermore, as a matter of personal preference, I spell out the words instead of using abbreviation. As such, I prefer phoneNumber over phoneNum, or phone_number over phone_num. Of course, there are always exceptions.
Hai Vu
  • 37,849
  • 11
  • 66
  • 93