Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3244012
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:30:02+00:00 2026-05-17T18:30:02+00:00

my homework assignment was to: write a function called findPattern() which accepts two strings

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:30:03+00:00Added an answer on May 17, 2026 at 6:30 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a basic homework assignment which looks like this: While input <> -1
For a homework assignment I was given a Card class that has enumerated types
Disclaimer: This is for a homework assignment, but the question is not regarding the
For homework, I was given the following 8 code fragments to analyze and give
(This is no homework and no work issue. It's just my personal interest/occupation and
I'm working through some homework and a question on a previous exam paper asks
So I've decided to try to solve my physics homework by writing some python
I've been working through a recent Computer Science homework involving recursion and big-O notation.
This one is a case of not doing your homework.:-) Apart from dynamic loading
I'm writing some excel-like C++ console app for homework. My app should be able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.