Skip to content Skip to sidebar Skip to footer

Python 3.6 - How To Translate A Telephone Number With Words

Trying to get this program to translate letters into numbers so a telephone number with words can be input and will output the number version. (1800GOTJUNK = 18004685865) Not sure

Solution 1:

str(transNum(phone)) should be str(transNum(ch)) And transNum doesn't need to iterate over its input, since it will only keep the last number (it is designed to have one single letter as input).


Solution 2:

I can't help you with the entire thing, but at least to make it a bit easier for you to reason about it. Use a dictionary to map the keys to values rather than killing some unicorns with all these ifs.

So you can do something like that

ch_num_map = {'a': 2, 'b': 2, 'c': 2, 'w': 9, 'z': 9} # you get the idea

then you can simply do:

ch_num_map.get('a')
# output: 2

Solution 3:

The problem here is that you're looping over the entire string in your transNum function. What you want is to pass a single character and get its number representation. Try this:

def transNum(ch):
    number = 1
    if ch.lower() in "abc":
        number = 2
    elif ch.lower() in "def":
        number = 3
    elif ch.lower() in "ghi":
        number = 4
    elif ch.lower() in "jkl":
        number = 5
    elif ch.lower() in "mno":
        number = 6
    elif ch.lower() in "pqrs":
        number = 7
    elif ch.lower() in "tuv":
        number = 8
    elif ch.lower() in "wxyz":
        number = 9
    return number


def translate(phone):
    newNum = ""
    for ch in phone:
        if ch in "abcdefghijklmnopqrstuvwxyz"
            newNum = newNum + str(transNum(ch))
        else:
            newNum = newNum + ch
    return newNum

I hope this helps.


Solution 4:

Let's take a look at this function:

def transNum(string):
    number = 1
    for ch in string:
        if ch.lower() in "abc":
            number = 2
        elif ch.lower() in "def":
            number = 3
        elif ch.lower() in "ghi":
            number = 4
        elif ch.lower() in "jkl":
            number = 5
        elif ch.lower() in "mno":
            number = 6
        elif ch.lower() in "pqrs":
            number = 7
        elif ch.lower() in "tuv":
            number = 8
        elif ch.lower() in "wxyz":
            number = 9
    return number

What this function does is take a string, loop over its characters, each time assigning the corresponding number to the variable number. At the end of the loop, it returns the variable number. So what this function is doing is essentially a bunch of useless work and then returning only what the last character in the string should correspond to as a number. What you want is to pass only a single character to this function and get rid of the for loop. Alternatively, you can create the translated string inside this function and return the full string rather than returning the number.


Solution 5:

I think should exist a more pythonic way, but at the least this should work for your case

def transNum(string):
    number = 1

    numberElements={
        "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,
    }

    for ch in string:
        number = numberElements[ch.lower()]
    return number

def translate(phone):
    newNum = ""
    for ch in phone:
        if ch.lower() in ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]:
            newNum = newNum + str(transNum(ch))
        else:
            newNum = newNum + ch
    return newNum

def main():
    phone = input("enter a phone number")
    noLetters = translate(phone)
    print("The number you entered: ", phone)
    print("Translates to: ", noLetters)

Post a Comment for "Python 3.6 - How To Translate A Telephone Number With Words"