Python 3.6 - 如何使用单词翻译电话号码

时间:2018-03-21 22:10:15

标签: python

尝试将此程序翻译成数字,以便输入带有单词的电话号码并输出数字版本。 (1800GOTJUNK = 18004685865)不知道我哪里出错但每个输出只给出最后一个字母的数字并重复所有数字的数字(1800adgjmptw = 18009999999)。非常感谢任何帮助,谢谢。

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

5 个答案:

答案 0 :(得分:5)

str(transNum(phone))应为str(transNum(ch)) 并且transNum不需要迭代其输入,因为它只保留最后一个数字(它被设计为只有一个字母作为输入)。

答案 1 :(得分:1)

我无法帮助你完成所有事情,但至少要让你更容易理解它。使用字典将键映射到值,而不是使用所有这些ifs杀死一些独角兽。

所以你可以做那样的事情

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

然后你就可以做到:

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

答案 2 :(得分:0)

这里的问题是你在transNum函数中循环遍历整个字符串。你想要的是传递一个字符并获得它的数字表示。试试这个:

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

我希望这会有所帮助。

答案 3 :(得分:0)

让我们来看看这个功能:

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

这个函数的作用是在字符串上循环,每次都将相应的数字赋给变量number。在循环结束时,它返回变量number。所以这个函数正在做的事情本质上是一堆无用的工作,然后返回 字符串中 last 字符对应的数字。你想要的是只将一个字符传递给这个函数并摆脱for循环。或者,您可以在此函数中创建已翻译的字符串并返回完整的字符串,而不是返回该数字。

答案 4 :(得分:0)

我认为应该存在更多的pythonic方式,但至少这应该适合你的情况

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)