Home > Back-end >  Need assistance on a problem set in edX course
Need assistance on a problem set in edX course

Time:05-02

I've been working on the below problem set for a long time and I'm still not able to pass the auto-grader. This question comes from online intro to python course I'm taking through edX. Could you please offer me some direction? Thank you.

Here's the question and my code: Write a function called "angry_file_finder" that accepts a filename as a parameter. The function should open the file, read it, and return True if the file contains "!" on every line. Otherwise the function should return False.

Hint: there are lots of ways to do this. We'd suggest using either the readline() or readlines() methods. readline() returns the next line in the file; readlines() returns a list of all the lines in the file.

#Write your function here!

def angry_file_finder(file_name):
   read = open(file_name, "r")
   lines = read.readlines()
   for line in lines:
      if not "!" in line:
          return False
      else:
          return True
   read.close()

Below are some lines of code that will test your function. You can change the value of the variable(s) to test your function with different inputs.

If your function works correctly, this will originally print: True

print(angry_file_finder("AngryFileFinderInput.txt"))

CodePudding user response:

HINT: Review you return statements.

If I've understood you problem correctly, you should return true if "!" is in every line and false otherwise. If this is the case, then the code you wrote will read the first line, and return false if "!" is not in the line, and true if it is.

This is the mistake. In either case you are returning true or false, so the program will always return after only checking the first line, the rest of the lines never get checked.

for line in lines:
    if not "!" in line:
              return False
          else:
              return True # <- this is the error, premature return

You want your program to check each line, returning false if "!" is not in it. If "!" is in the line, then check the next line. Only once you have checked all lines for "!" can you that every line has a "!" in it.

I'd suggest you have code more similar to

for line in lines:
   if not "!" in line:
      read.close()  # close the file before returning
      return False
read.close()
return True # <- outside of loop so will only return true after loop is finished

That way your program checks every line for a "!" and only produces true if "!" is in every line, and false if "!" is missing from any line.

I hope this helps and good luck with your learning :)

CodePudding user response:

def angry_file_finder(file_name):

   file = open(file_name, "r")
   lines = file.readlines()
   file.close()  # you can close the file now because you have
                 # already read all its content and stored it in
                 # lines

   for line in lines:
      # Return false if any line does not contain "!"
      if not "!" in line:
          return False
   
   # Only after checking all the lines you can be sure that they
   # all contain at least one "!" character
   return True
  • Related