字母数字 phone 翻译器损坏

Alphanumerical phone translator broken

我正在尝试制作一个基本的(我是新手)程序来翻译 phone 号码,就像您在电视 ex 上看到的那样。 555-PLZ-HELP 到实际的 phone 号码。用户将输入数字,程序将以相同的格式 return 555-555-5555。我正在使用实际的 telephone 按钮等价物。

phoneNum = input("Please enter a number in the format of XXX-XXX-XXXX: ")
newNum = '' 

for ch in phoneNum[:]:
    if ch == 'A' or ch == 'B' or ch == 'C':
            ch == '2'
    elif ch == 'D' or ch == 'E' or ch == 'F':
            ch = '3'
    elif ch == 'G' or ch == 'H' or ch == 'I':
            ch = '4'
    elif ch == 'J' or ch == 'K' or ch == 'L':
            ch = '5'
    elif ch == 'M' or ch == 'N' or ch == 'O':
            ch = '6'
    elif ch == 'P' or ch == 'Q' or ch == 'R' or ch == 'S':
            ch = '7'
    elif ch == 'T' or ch == 'U' or ch == 'V':
            ch = '8'
    elif ch == 'W' or ch == 'X' or ch == 'Y' or ch == 'Z':
            ch = '9'

newNum += ch

print(newNum)

据我所知,这段代码有两处错误。 1 - 行:

newNum += ch

应该在循环下

2 - 行:

ch == '2'

应该是

ch = '2'

虽然需要一些(一次性)设置,但使用内置字符串 translate()' method, but doing so requires creating a "translate table". Fortunately there's also a string class method named maketrans 可以很容易地做到这一点。

#!/usr/bin/env python3
# 

mapping = {
    'ABC': '2',
    'DEF': '3',
    'GHI': '4',
    'JKL': '5',
    'MNO': '6',
    'PQRS': '7',
    'TUV': '8',
    'WXYZ': '9',
}

# Create string translate table from the mapping.
xlate_table = {}
for group, num in mapping.items():
    for letter in group:
        xlate_table[letter] = num
xlate_table = str.maketrans(xlate_table)

#phone_num = input("Please enter a number in the format of XXX-XXX-XXXX: ")
phone_num = '555-PLZ-HELP'

new_num = phone_num.translate(xlate_table)
print(new_num)  # --> 555-759-4357

我尝试添加一个验证循环并将其隐藏起来

letters = ['ABCDEFGHIJKLMNOPQRSTUVWXYZ']
numbers = ['1234567890']
dash = ['-']
newNum = '' 
#Ask for user to input alphanumeric phone number and a variable for our loop
def numTran():
    phoneNum = input("Enter the number in the format of XXX-XXX-XXXX (all CAPS): ")
    for ch in phoneNum[:]:
        if ch != (letters) or ch != (numbers) or ch!= (dash):
            print('Please submit a valid response')
            numTran()
        #create a loop and index our input, reassign the letters to equal appropriate
        #number
        for ch in phoneNum[:]:
            if ch == 'A' or ch == 'B' or ch == 'C':
                    ch = '2'
            elif ch == 'D' or ch == 'E' or ch == 'F':
                    ch = '3'
            elif ch == 'G' or ch == 'H' or ch == 'I':
                    ch = '4'
            elif ch == 'J' or ch == 'K' or ch == 'L':
                    ch = '5'
            elif ch == 'M' or ch == 'N' or ch == 'O':
                    ch = '6'
            elif ch == 'P' or ch == 'Q' or ch == 'R' or ch == 'S':
                    ch = '7'
            elif ch == 'T' or ch == 'U' or ch == 'V':
                    ch = '8'
            elif ch == 'W' or ch == 'X' or ch == 'Y' or ch == 'Z':
                    ch = '9'
        #create and accumulator for our variable newNum
            newNum += ch

        print(newNum)
numTran()