我正在尝试完成将字母转换为电话号码序列的代码。我需要做的是例如 JeromeB 到 537-6632。我还需要程序在最后一个可能的数字之后切断字母翻译。因此,例如在 1-800-JeromeB 之后,即使我写在 1-800-JeromeBrigham 中,它也不会编码。问题是,虽然我不明白如何将其包含在我的代码中。我不知道把最后一个字母剪掉放在破折号里。我目前拥有的是这个
alph = ['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']
num =[2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9]
phone = raw_input('enter phone number ').lower()
s = ""
for index in range(len(phone)):
if phone[index].isalpha():
s = s + str(num[alph.index(phone[index])])
else:
s = s + phone[index]
print s