基本上,我正在创建一个用于转换电话号码的程序 字母,只是简单的数字。我设法做到了这一点, 但是,如果您输入数字,它们将被删除,并且只能转换 信件被退回。
例如,如果您输入1800BUYNOW
,而不是返回1800289669
,
它只返回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()
答案 0 :(得分:1)
只需替换
if char in ['A','B','C']:
通过
if char in ['A','B','C','1']:
等等。
实际上你最后应该有一个报告未知字符的其他内容并用elif替换if。