Finding and Fixing Errors

In this part of the lab, you'll be finishing up lab2.py, and then working on a Python file full of errors. We'll first explain how to find errors, and how to understand the error messages. Following that, you'll solve a few errors on your own.





Running the lab2.py file

First, let's try running the lab2.py file you have created. Save your file and exit emacs. Before running your program, make sure you enable Python 3:

       scl enable python33 bash

You'll know that Python 3 was successfully enabled if the prompt in the command line changes from something like "linux1[13]%" to "bash-4.1$". You must enable Python 3 before you run your program, but you only need to do it once per time you log on to GL. Other commands (like ls, submit, and emacs) will still work when Python 3 is enabled.

Go ahead and run your program by using the python command:

       python lab2.py

If you copied everything exactly, your program won't run — instead, you'll get an error message.





Reading error messages

Your error message should look something like this:

linux1[13]: scl enable python33 bash
bash-4.1$ python lab2.py
  File "lab2.py", line 14
    print("Hello, my name is YOUR_NAME)
                                      ^
SyntaxError: EOL while scanning string literal
bash-4.1$

There are a couple of key pieces that are present in this error message: "

  1. We know the name of the file and the line on which the error occurred. In this case, it's our lab2.py file, and the error is on line 14.
  2. Python has attempted to pinpoint the error even further for us, using the "^" symbol you can see on the second-to-last line.
  3. Python has told us what kind of error it is, and some details:
    1. It is a syntax error
    2. Python reached EOL (End of Line)
    3. While scanning a "string literal"

Python won't be able to spot logical errors for you, but it tends to be very good at pinpointing syntax errors. However, it sometimes won't notice an error has occurred until a few lines after the actual error. If you don't see anything wrong with the line Python has indicated, try looking at the lines directly before it for anything odd. (Think about it like this: if you made a wrong turn while driving, you might not notice your mistake until you came to an unexpected dead end. But the dead end itself is not the error — it's one of the turns you took before that.)





Fixing an error

Hopefully you've spotted the error already — the print() statement is missing the closing quotation marks. To fix it, we'll need to open lab2.py again for editing.

Once you do that, take a look at the bottom of the screen, and you should see something like this:




-UUU:----F1  lab1.py        All  L1      (Python)----------

The "L1" you see there (second from the right) stands for "Line 1" — the error was on line 14, so move your cursor down until you've reached "L14" instead. Fix the error by adding the closing quotation mark, and save and exit again.

Try running your program again — if you fixed the error correctly, it should work and display the greeting you wrote.

If it doesn't work, use what you've learned to find the "new" error and fix it. Sometimes Python will only display the first error it finds, so you may find yourself having to do this process multiple times when working on your assignments. (If there is more than one error, start with the message at the bottom first. Fix that one error, and then try to run your program again.)





Applying your debugging skills

Now it's time to put your bug fixing abilities to the test! First you'll copy a file called errors.py into your lab2 folder using the cp command. (Don't forget to include the period at the end of the command!)

       cp /afs/umbc.edu/users/k/k/k38/pub/cs201/errors.py .

Before you jump into trying to fix the bugs, take a moment to read the code and figure out what the program should be doing. (The original programmer didn't use comments, but they used good names for their variables, so it should be doable.) Then use your new knowledge of finding and fixing bugs to update the errors.py file to run without any errors.





Testing your fixes

Test your fixed errors.py Python program with different inputs to ensure that it runs correctly. (Try big numbers and negative numbers. An integer is a whole number, so your program won't work if you give it decimals or letters.) Make sure you have enabled Python 3 before testing:

       scl enable python33 bash

If your testing finds a bug, fix it, and try running the program again. You might have to do this multiple times before it is fixed.

Once errors.py works, add these two lines to the file's header comment block:

       # Fixed by: YOUR_NAME
       # Date fixed: TODAYS_DATE

The headers of your files (the block of comments at the top) are very important. Double check that you have correctly completed the headers for the errors.py and lab2.py files.