简体   繁体   English

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

[英]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 where Im going wrong but every output just gives whatever the last letter is and repeats its number for all numbers (1800adgjmptw = 18009999999). (1800GOTJUNK = 18004685865)不知道Im哪里出错了,但是每个输出都给出最后一个字母,然后对所有数字重复其数字(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()

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

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. 使用字典将键映射到值,而不是用所有这些if杀死某些独角兽。

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

The problem here is that you're looping over the entire string in your transNum function. 这里的问题是您正在遍历transNum函数中的整个字符串。 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. 我希望这有帮助。

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 . 该函数的作用是获取一个字符串,遍历其字符,每次将对应的数字分配给变量number At the end of the loop, it returns the variable number . 在循环结束时,它返回变量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. 您想要的是仅将单个字符传递给此函数,并摆脱for循环。 Alternatively, you can create the translated string inside this function and return the full string rather than returning the number. 或者,您可以在此函数内创建转换后的字符串,然后返回完整的字符串,而不是返回数字。

I think should exist a more pythonic way, but at the least this should work for your case 我认为应该以更Python化的方式存在,但至少这应该适合您的情况

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM