Additional Help

On this page you can find additional hints and help for if you get stuck on the lab. We encourage you to use this information only after giving the problem a few minutes of thought on your own.





Table of Contents
  1. Getting a "FileNotFound" error?
  2. Getting a "NameError" error?
  3. Getting a "TypeError" error?
  4. Having trouble reading in the lines individually?
  5. Not sure how to parse the data on each line?
  6. Does your announcements.txt file come out blank?
  7. Is your announcements.txt file a jumbled mess?
  8. Is your code not doing what it should, and you're having trouble figuring out exactly where it's going wrong?



Getting a "FileNotFound" error?

If you are getting an error that looks something like the following FileNotFoundError: [Errno 2] No such file or directory
make sure that you have downloaded the roster.txt file, and that you have spelled the filename correctly when you attempt to open it.

(back to top)
































Getting a "NameError" error?

If you are getting an error that looks something like the following NameError: global name 'resultsFile' is not defined
make sure that you opened the output file before you started writing to it! That is how we initialize a file variable for Python — we have to tell it what filename it's supposed to output to before we can use write() on it.

(back to top)
































Getting a "TypeError" error?

If you are getting an error that looks something like the following TypeError: unorderable types: str() > int()
remember that everything read in from a file initially comes in as a string. You will need to cast numbers to the appropriate type (float or int) before comparing or saving them.

(back to top)
































Having trouble reading in the lines individually?

As stated in the Pre Lab, if you want to extract or examine data from a file, using a for loop or one of the read() family of functions to iterate over the lines of the file makes the most sense. Click here to go directly to that section of the Pre Lab.

(back to top)
































Not sure how to parse the data on each line?

There are several key pieces of information on each line of the roster.txt file. You can use the split() command to break each record up into individual tokens.

Since you know the format of the file, you can assign each piece to a variable:
        major, gpa, lname, fname = [splitting the line]


Remember that split() uses whitespace as its default delimiter. However, you can give it a different character to split on:
        line.split(";")

(back to top)
































Does your announcements.txt file come out blank?

The most likely cause of this is that you are failing to write() out each line of output.

(back to top)
































Is your announcements.txt file a jumbled mess?

There are two possible problems you could be encountering.

The first is solved by making sure that you are printing out newlines at the end of every entry. Otherwise your output will look like this:
        Brian Lewis is graduating with a degree in CHEMNeville Owens is graduating with a degree in POLILuke Pugh is graduating with a degree in CMSCHiroko

The second is solved by making sure you are stripping the whitespace from the string when you read it in. If you don't, your output will look like the following:

Brian
 Lewis is graduating with a degree in CHEM
Neville
 Owens is graduating with a degree in POLI
Luke
 Pugh is graduating with a degree in CMSC

(back to top)
































Is your code not doing what it should, and you're having trouble figuring out exactly where it's going wrong?

If you're still stuck, you can try using a "debug statement" or two. These are print() statements that give you a bit more information on what exactly is going on. For example, you might want to see what each line looks like as it is read in:
    print(eachLine)

Or your might want to see the GPA being compared:
    print("Current GPA is", currGPA, "and max GPA is", maxGPA)

If you place either of these print() statements inside the appropriate for loop, it will show you what is going on in the "background" of your program. Each time the for loop is run, the information in your debug statement will be printed to the screen, allowing you to trace what happens with each iteration of the for loop.

Debug statements can be anything that helps you figure out what your program is actually doing. Just don't forget to remove them when you're done!

(back to top)