Python版,里面还有些不完善的点,欢迎指正
#!/usr/bin/env python """ transform word to number like mobile phone """ version=0.2 author='liwei.an' mod="abcdefghijklmnopqrstuvwxyz" mod=mod+mod.upper() def check_word(word): for i in range(2): for j in word: if j not in mod: print("number in the 'word',please input again!") word=raw_input("input a word: ") else: break else: break map={'x0':'','x1':'','x2':"abc",'x3':'def','x4':'ghi','x5':'jkl','x6':'mno','x7':'pqrs','x8':'tuv','x9':'wxyz'} def word_to_number(word): number="" for i in word.lower(): for j in map.keys(): if i in map[j]: number +=j number=number.replace("x","") return number if __name__ == '__main__': word=raw_input('input a word: ') check_word(word) number = word_to_number(word) print("{0} => {1}".format(word,number))
#Python##学习路径#