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.
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.
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.
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.
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.
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]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.
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
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!