将整数存储在输入中,与字符分开

Store integers in an input, separately to characters

基本上,我正在创建一个程序,将 phone 数字转换为 中的字母,变成纯数字。我设法做到了这一点, 但是,如果您输入数字,它们将被删除并且只有转换后的数字 信件被退回。

例如,如果您输入 1800BUYNOW,而不是返回 1800289669, 它 returns 只是 289669.

有什么帮助吗?

#phone number converter
#v0.8
#this will convert phone numbers with letters in, into just phone numbers
#i.e. 1800BUYNOW should output as 1800289669

import time
import string

dicti = []

#######################################################################

def translate():
    print('Welcome to the telephone unconfuzzler!')
    num = input('''Type the number you which to deconfuzzle.
''')
    for char in (num):
        if char in ['A','B','C']:
            dicti.append(2)
        if char in ['D','E','F']:
            dicti.append(3)
        if char in ['G','H','I']:
            dicti.append(4)
        if char in ['J','K','L']:
            dicti.append(5)
        if char in ['M','N','O']:
            dicti.append(6)
        if char in ['P','Q','R','S']:
            dicti.append(7)
        if char in ['T','U','V']:
            dicti.append(8)
        if char in ['W','X','Y','Z']:
            dicti.append(9)
    finish()

#######################################################################

def finish():
     time.sleep(0.5)
     print('''Converting
...''')
     time.sleep(1)
     print('''Here is your deconfuzzled number:''')
     print (*dicti, sep='')
     end()

#######################################################################

def end():
    conta = input('Would you like to convert another number?')
    if conta in ['y', 'Y', 'yes', 'Yes', 'YES']:
        translate()
    elif conta in ['n', 'N', 'no', 'No', 'NO']:
        conta2 = input('Are you sure you would like to quit?')
        if conta2 in ['y', 'Y', 'yes', 'Yes', 'YES']:
            exit()
        elif conta2 in ['n', 'N', 'no', 'No', 'NO']:
            end()


#######################################################################

translate()

直接替换

   if char in ['A','B','C']:

来自

   if char in ['A','B','C','1']:

等等。

实际上你应该在末尾有一个 else 来报告未知字符并将 if 替换为 elifs。