1

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 where Im going wrong but every output just gives whatever the last letter is and repeats its number for all numbers (1800adgjmptw = 18009999999). Any help would be greatly appreciated, thanks.

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


def translate(phone):
    newNum = ""
    for ch in phone:
        if ch 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(phone))
        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)

main()
nickchamb727
  • 11
  • 1
  • 2
  • Probably you need to do `ch.lower() in ['a', 'b', 'c']` on your transNum method – Jacobo Mar 21 '18 at 22:13
  • Change `newNum = newNum + str(transNum(phone))` to `newNum = newNum + str(transNum(ch))` – Andrei Cioara Mar 21 '18 at 22:15
  • 2
    The easy way: `noletters = phone.translate(str.maketrans( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "2223334445556667777888999922233344455566677778889999")) ` – Robᵩ Mar 21 '18 at 22:16

5 Answers5

5

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).

matli
  • 27,922
  • 6
  • 37
  • 37
1

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
  • While this is indeed a solution, it is not as readable as the if-else construct (with which there is nothing wrong). – trk Mar 21 '18 at 22:36
0

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.

trk
  • 342
  • 4
  • 13
0

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.

enumaris
  • 1,868
  • 1
  • 16
  • 34
0

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)
francisco
  • 103
  • 2
  • 12
  • Also you can use `list(string.ascii_lowercase)` to remove the line `["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"]` You can check this in [link](https://stackoverflow.com/questions/16060899/alphabet-range-python) – francisco Mar 21 '18 at 22:40