Search code examples
python-3.xstringfile-iotext-filesspell-checking

Simple Python File I/O spell check program


For a class I have to create a simple spell checking program that takes two files as inputs, one containing correctly spelled words and one containing a paragraph with a few misspelled words. I thought I had it figured out but I am getting an error I have never seen before. When the program finishes it gives the error:

<function check_words at 0x7f99ba6c60d0>

I have never seen this nor do I know what it means, any help in getting this program working would be appreciated. Program code is below:

import os
def main():
    while True:
        dpath = input("Please enter the path to your dictionary:")
        fpath = input("Please enter the path to the file to spell check:")
        d = os.path.isfile(dpath)
        f = os.path.isfile(fpath)

        if d == True and f == True:
            check_words(dpath, fpath)
            break

    print("The following words were misspelled:")
    print(check_words)

def linecheck(word, dlist):
    if word in dlist:
        return None
    else:
        return word

def check_words(dictionary, file_to_check):
    d = dictionary
    f = file_to_check
    dlist = {}  
    wrong = []  


    with open(d, 'r') as c:
        for line in c:
            (key) = line.strip()
            dlist[key] = ''

    with open(f, 'r') as i:
        for line in i:
            line = line.strip()
            fun = linecheck(line, dlist)
            if fun is not None:
                wrong.append(fun)

    return wrong

if __name__ == '__main__':
    main()

Solution

  • Yes, don't do print(check_words), do print(check_words())

    Furthermore, change check_words(dpath, fpath) to misspelled_words = check_words(dpath, fpath)

    And change print(check_words) to print(misspelled_words)

    Final code (with a few modifications):

    import os
    def main():
        while True:
            dpath = input("Please enter the path to your dictionary: ")
            fpath = input("Please enter the path to the file to spell check: ")
            d = os.path.isfile(dpath)
            f = os.path.isfile(fpath)
    
            if d == True and f == True:
                misspelled_words = check_words(dpath, fpath)
                break
    
        print("\nThe following words were misspelled:\n----------")
        #print(misspelled_words) #comment out this line if you are using the code below
    
        #optional, if you want a better looking output
    
        for word in misspelled_words:   # erase these lines if you don't want to use them
            print(word)                 # erase these lines if you don't want to use them
    
        #------------------------ 
    
    
    def linecheck(word, dlist):
        if word in dlist:
            return None
        else:
            return word
    
    def check_words(dictionary, file_to_check):
        d = dictionary
        f = file_to_check
        dlist = {}  
        wrong = []  
    
    
        with open(d, 'r') as c:
            for line in c:
                (key) = line.strip()
                dlist[key] = ''
    
        with open(f, 'r') as i:
            for line in i:
                line = line.strip()
                fun = linecheck(line, dlist)
                if fun is not None:
                    wrong.append(fun)
    
        return wrong
    
    if __name__ == '__main__':
        main()