Member Avatar for lightning18

i currently have this piece of code
i need to create a dictionary that prints the incorrect word and the line number where it is incorrect.
words [] # is my txt file
text1 [] # is my list of incorrect words
i am new to programming and dont know much syntax so hope you can help and tell me what i have done wrong or need to change cheers.

d = {}
counterline = 0
for lines in words:
    text1 = lines.split()
    counterline += 1
    if text1 not in d:
        d[text1] = [counterline]
    if text1 in d:
        d[text1].append[counterline]
        print(text1,    d[counterline])

Recommended Answers

All 2 Replies

This should get you started, it will have to be tweaked depending on the infile that you are using.

def main():
    
    d = {}
    text1 = ["dog","cat"]
    infile = open("words.txt","r")  #Opening words.txt, r is for read
    counter = 0
    
    for line in infile:
        words = line.split(",") #Using "," for delimiter
        counter += 1
        for word in words:
            if word in text1:   #If the word is incorrect
                d[word] = counter   #Adds to the dictionary
    
    #printing out the dictionary
    print "The Incorrect Words are:\n"
    for iWord, count in d.iteritems():
        print "%s Found at line number %i" % (iWord,count)
        
if __name__ == '__main__':
    main()
Member Avatar for lightning18
import sys
import string

words = []
infile = open(sys.argv[1], 'r').read()
for punct in string.punctuation:
    infile = infile.replace(punct, "")
    words = infile.split()

dict = open(sys.argv[2], 'r').read()
dictset = []
dictset = dict.split()

text1 = []
text1 = list(set(words) - set(dictset))
text1 = [text.lower() for text in text1]
text1.sort()

def main():
    d = {}
    counter = 0
    for line in infile:
        soccer = line.split(",")
        counter += 1
        for word in words:
            if word in text1:
                d[word] = counter
    print("The Incorrect Words are:\n")
    for iWord, count in d.iteritems():
        print("%s Found at line number %i" % (iWord, count)

this is what ive got but it gives me a syntx error:

File spellcheck2.py, line 47
     ^
syntax error invalid syntax
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.