#read the list of words from the file file1=open("C:/Users/user/Downloads/EnglishWords4Letters.txt",'r') #store the list of words in a python list words_list=list(file1.read().split('\n')) #take phone number as input from user number=input('Enter an 8-digit number\n') #store the keypad letters in key-letters format letters={'2':'ABC','3':'DEF','4':'GHI','5':'JKL','6':'MNO','7':'PQRS','8':'TUV','9':'WXYZ'} #extract the last four digits of phone number for translation last_four=number[-4:] #temporary list to store possible translations for given number #list i initially a copy of original words_list provided prev_list=words_list[:-1] k=0 for i in last_four: new_list=[] #if number can no more get translated if i not in letters.keys(): print('Translation failed') break #check for the possible combinations stepwise for each number in sequence for j in range(len(letters[i])): temp=letters[i][j].lower() for word in prev_list: if word[k]==temp: new_list.append(word) k+=1 prev_list=new_list[:] #if no more translations can be obtained if not prev_list: print('Translation failed') break else: #if there are possible translations print them all for i in prev_list: print(number[:-4]+i.upper())
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)