【问题标题】:Alphanumerical phone translator broken字母数字电话翻译器坏了
【发布时间】:2020-05-03 00:10:56
【问题描述】:

我正在尝试制作一个基本(我是新人)程序来翻译您在电视前看到的电话号码。 555-PLZ-HELP 到一个实际的电话号码。用户将输入数字,程序将以相同的格式 555-555-5555 返回它。我正在使用实际的电话按钮等效项。

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)

【问题讨论】:

    标签: python translate alphanumeric


    【解决方案1】:

    据我所知,这段代码有 2 个问题。 1 - 行:

    newNum += ch
    

    应该在循环之下。

    2 - 行:

    ch == '2'
    

    应该是

    ch = '2'
    

    【讨论】:

    • 天哪,谢谢,我今天已经写了好几个小时的代码了。我想有时你只需要别人看看你在头脑被抹去时做了什么,哈哈。非常感谢!
    • 很高兴为您提供帮助,您也可以将此标记为答案,以便其他人都知道您不需要更多帮助。
    【解决方案2】:

    虽然需要一些(一次性)设置,但使用内置字符串translate()' 方法很容易做到这一点,但这样做需要创建一个“翻译表”。幸运的是,还有一个名为 maketrans 的字符串类方法可以帮助做到这一点。

    #!/usr/bin/env python3
    # https://stackoverflow.com/questions/61568008/alphanumerical-phone-translator-broken
    
    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
    

    【讨论】:

      【解决方案3】:

      我尝试添加一个验证循环并将其关闭

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-16
        • 2011-01-15
        • 2017-08-19
        • 2016-02-17
        • 2016-03-21
        • 1970-01-01
        相关资源
        最近更新 更多