How do I use permutation to find all possible words from an input?
       
I am doing a project for my programming class, here are the 5 things that I am working towards. Thanks for your help!

    
1)list of valid words will be provided in a words.txt

2)ask for input that only accepts a combination of 2-8 letters

3)Ex. input would be "polo"

4)Find all possible valid words of this input from inside words.txt

5)Possible outcomes, "poo" "pool" "loop" "polo"
        
        import random
        import itertools
        
        file_name='words.txt'
        DEBUG=True
        
        def load_words():
            
            try:
                f=open(file_name,'r')
                str1=f.read()
                f.close()
                
            except:
                print('Problem opening the file',file_name)
        
            list1=[]
            list1=str1.split()
        
            if DEBUG:
                
                length=len(list1)
                
                print('Number of words is',length)
                print(list1[0],list1[length//2],list1[-1])
        
                for i in range(10):
                    print(list1[random].randint(1,length))
        
            return(list1)
        
        def is_valid(str1,list1):
            valid=False
            count=0
            if str1 in list1:
                valid=True
            return valid
        
        def generate(letters):
            print(len(letters))
            count=1
            answers=[]
            for length in range(2,len(letters)+1):
                for x in itertools.permutations(letters,length):
                    word=''
                    for let in x:
                        word+=let
                    print(word)
                    if is_valid(word):
                        answers.append(word)
                    count+=1   
                print(count,x)
                return(answers)
                    
        
        def main():
            words=load_words()
            generate('abcde')
            guess=input('Check a word')
            
            if is_valid(guess.upper(),words):
                print(guess,'is a real word')
            else:
                print(guess,'is not a real word')
            
        main()


How do I use permutation to find all possible words from an input?
===

       
I am doing a project for my programming class, here are the 5 things that I am working towards. Thanks for your help!

    
1)list of valid words will be provided in a words.txt

2)ask for input that only accepts a combination of 2-8 letters

3)Ex. input would be "polo"

4)Find all possible valid words of this input from inside words.txt

5)Possible outcomes, "poo" "pool" "loop" "polo"
        
        import random
        import itertools
        
        file_name='words.txt'
        DEBUG=True
        
        def load_words():
            
            try:
                f=open(file_name,'r')
                str1=f.read()
                f.close()
                
            except:
                print('Problem opening the file',file_name)
        
            list1=[]
            list1=str1.split()
        
            if DEBUG:
                
                length=len(list1)
                
                print('Number of words is',length)
                print(list1[0],list1[length//2],list1[-1])
        
                for i in range(10):
                    print(list1[random].randint(1,length))
        
            return(list1)
        
        def is_valid(str1,list1):
            valid=False
            count=0
            if str1 in list1:
                valid=True
            return valid
        
        def generate(letters):
            print(len(letters))
            count=1
            answers=[]
            for length in range(2,len(letters)+1):
                for x in itertools.permutations(letters,length):
                    word=''
                    for let in x:
                        word+=let
                    print(word)
                    if is_valid(word):
                        answers.append(word)
                    count+=1   
                print(count,x)
                return(answers)
                    
        
        def main():
            words=load_words()
            generate('abcde')
            guess=input('Check a word')
            
            if is_valid(guess.upper(),words):
                print(guess,'is a real word')
            else:
                print(guess,'is not a real word')
            
        main()

