-2

I'm doing an assignment for class, where we're supposed to:

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. Use a loop to iterate over each character in the string. Write a function that converts a letter to the correct number according to the mapping listed above. Call this function in the loop to convert the characters.

This is the code I have so far:

# write function that converts letter to number
def num_trans(number):
    num = number.upper()
    if char == "A" or char == "B" or char == "C":
        num.replace(char,"2")
    elif char == "D" or char == "E" or char == "F":
        num.replace(char,"3")
    elif char == "G" or char == "H" or char == "I":
        num.replace(char,"4")
    elif char == "J" or char == "K" or char == "L":
        num.replace(char,"5")
    elif char == "M" or char == "N" or char == "O":
        num.replace(char,"6")
    elif char == "P" or char == "Q" or char == "R" or char == "S":
        num.replace(char,"7")
    elif char == "T" or char == "U" or char == "V":
        num.replace(char,"8")
    elif char == "W" or char == "X" or char == "Y" or char == "Z":
        num.replace(char,"9")
# ask for user input phone number
numb = input("Please input a number in the format XXX-XXX-XXXX: ")
# change letters to numbers
    # use loop to go over each character
for char in numb:
    new_num = num_trans(numb)
# print number
print(new_num)

Anything I enter in the input, I just get "None". I have no idea how to fix this. Please help

Asher
  • 11
  • 1
  • 1
    Your `num_trans` function doesn't return anything. Do you want to be replacing the `number` argument with the result of the `replace` call? Your code is pretty confusing, the function should probably take `char` as an argument too, rather than using it as a global variable. – Blckknght May 09 '21 at 01:16
  • 2
    You need to return a value. – Buddy Bob May 09 '21 at 01:18
  • Does this answer your question? [Python 3.6 - How to translate a telephone number with words](https://stackoverflow.com/questions/49417352/python-3-6-how-to-translate-a-telephone-number-with-words) – zerecees May 09 '21 at 01:19
  • Try using a debugger. – TomServo May 09 '21 at 01:24
  • Does this answer your question? [Function returns None without return statement](https://stackoverflow.com/questions/7053652/function-returns-none-without-return-statement) – Gino Mempin May 09 '21 at 03:21

3 Answers3

1
def num_trans(number):
    num = number.upper()
    if num == "A" or num == "B" or num == "C":
        return '2'
    elif num == "D" or num == "E" or num == "F":
        return '3'
    elif num == "G" or num == "H" or num == "I":
        return '4'
    elif num == "J" or num == "K" or num == "L":
        return '5'
    elif num == "M" or num == "N" or num == "O":
        return '6'
    elif num == "P" or num == "Q" or num == "R" or num == "S":
        return '7'
    elif num == "T" or num == "U" or num == "V":
        return '8'
    elif num == "W" or num == "X" or num == "Y" or num == "Z":
        return '9'
    else:
        return str(num)

def new_function():
    new_number = ''
    for char in '555-GET-FOOD':
        new_number += num_trans(char)
    print(new_number)
if __name__ == '__main__':
    new_function()

output

555-438-3663
GeorgesAA
  • 153
  • 5
0

You can do more of what you are trying to achieve by returning a value from num_trans:

def num_trans(number):
    num = number.upper()
    if char == "A" or char == "B" or char == "C":
        return "2"
    elif char == "D" or char == "E" or char == "F":
        return "3"
    elif char == "G" or char == "H" or char == "I":
        return "4"

and so on - but you will also need to reassemble these values as they come back from the function, and handle other characters like numbers and dashes.

Joffan
  • 1,485
  • 1
  • 13
  • 18
0

There are few things you may need to know:

  1. strings are immutable, replace() does not change the strings in-place, which means if you don't save your result in a new variable, you just get the original one.
  2. after you save the new result, you need to return it, so the variable outside can assign its value, otherwise the function just return None.
  3. although your code can run, having a variable (char in this case) that is not defined in function parameter is really confusing.

I make only small changes of your code to make it works, but it can be further improved:

def num_trans(number):
    num = number.upper()
    if char == "A" or char == "B" or char == "C":
        # save result after replace
        num = num.replace(char,"2")
    elif char == "D" or char == "E" or char == "F":
        num =  num.replace(char,"3")
    elif char == "G" or char == "H" or char == "I":
        num = num.replace(char,"4")
    elif char == "J" or char == "K" or char == "L":
        num = num.replace(char,"5")
    elif char == "M" or char == "N" or char == "O":
        num = num.replace(char,"6")
    elif char == "P" or char == "Q" or char == "R" or char == "S":
        num = num.replace(char,"7")
    elif char == "T" or char == "U" or char == "V":
        num = num.replace(char,"8")
    elif char == "W" or char == "X" or char == "Y" or char == "Z":
        num = num.replace(char,"9")
    # return it
    return num
numb = input("Please input a number in the format XXX-XXX-XXXX: ")
new_num = numb
for char in numb:
    # use num_trans(new_num) to replace the string recursively
    # otherwise num_trans(numb) only do the last change
    new_num = num_trans(new_num)
print(new_num)

Output:

555-438-3663
adamkwm
  • 1,155
  • 2
  • 6
  • 18