views:

115

answers:

3

Hi all, my homework assignment was to: "write a function called findPattern() which accepts two strings as parameters, a filename and a pattern. The function reads in the file specified by the given filename and searches the file’s contents for the given pattern. It then returns the line number and index of the line where the first instance of this pattern is found. If no match is found, your function should return -1 for both the line number and index."

I was fairly certain that my code was accurate until it would execute the first commands and then just ignore the rest of the code. I tried a couple different ways of writing it, but all three yielded the result of...not working.

I'll post the two relevant codes below:

Code 1:

def findPattern (filename, pattern):
    f=open(filename)

    linecount = 0
    lettercount = 0


    for line in f:
        lineCount +=1
        for letter in range(len(line)):
            if line(letter)==pattern:
                letterCount+=1
                return[lineCount,line]
    return "Did not find " + pattern

Code 2:

print
filename = raw_input("Enter a file name: ")
pattern = raw_input("Enter a pattern: ")

def findPattern (filename,pattern):
    f=open(filename)

    lineCount = 0
    letterCount = 0

    for line in f:
        lineCount +=1
        for letter in range(len(line)):
            if line(letter)==pattern:
                letterCount+=1
                print ("Found pattern " + pattern + " at " + str((lineCount, letter)))

I think code 2 would be more likely to work but it isn't yielding any results. Any input would be appriciated.

-Thanks!

A: 

You're calling line as a function but it is a string. Use pattern.find(line) on each line to find your pattern.

Gintautas Miliauskas
+1  A: 

Your variable names are misspelled: linecount vs. lineCount, lettern vs. letter. Python doesn't always warn against this type of error. If this is just a copying error, then line(letter) is the error: an index is given by []. What kind of pattern are you searching for, a single character or a string? line[letter] will only return a single character.

Next time, please post not only the code and that it gives an error, but also what kind of error. Most Python errors result in exceptions being thrown (such as TypeError), which can tell you (and us) a lot about what's been going wrong.

larsmans
Well the thing was it didn't yield anything, upon typing in the raw_inputs it would just do this: >>> So thank you for the prompt response, I actually didn't notice the typos.
compsciencenub
Also, I am searching for a string. Unfortunately, I was going by code from a discussion class, so I wasn't quite sure what was what.
compsciencenub
Are these the complete scripts? Then the solution is quite simple: you're not calling the `findPattern` function, just defining it.
larsmans
Yes, both of them are complete. The only thing that's missing was the findPattern("filename", "pattern"). Thanks again for responding, I'm new at the whole python thing, so any help is appreciated.
compsciencenub
Okay, I called the findPattern function and finally got a real error: File "C:\Program Files (x86)\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", line 18, in <module> File "C:\Program Files (x86)\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", line 12, in findPatternUnboundLocalError: local variable 'lineCount' referenced before assignment>>>
compsciencenub
That's one of the spelling errors I warned you about.
larsmans
I fixed the spelling error though.Now we have a new error:Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", line 18, in <module> File "C:\Program Files (x86)\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", line 14, in findPatternTypeError: 'str' object is not callable
compsciencenub
That's the second error I pointed out, function call on a string-typed object. Use `[]`.
larsmans
A: 

So did you manage to get it working properly, and can you post the finished code? I have something similar to work on myself...thanks

anewone1010