Session 9: Making Mistakes

Session 9: Making Mistakes#

As you learn to code you will make mistakes - but Python will do its best to tell you where you went wrong and maybe how to fix it. This session is concerned with interpreting the error messages it generates.

Task 1#

  1. In a terminal window, navigate to your PHAR2062/Session 9 - Making Mistakes folder.

  2. Make a copy of the file “hello.py” from Session 1 and put it in this folder.

  3. Open this copy in your favourite text editor, and now deliberatly insert an error. Find the line:

    datafile = open('greeting.txt')

    and remove the quotes round the name of the file:

    datafile = open(greeting.txt)

  4. Save the edited file as ‘hello_bad1.py’ and try to run it in the usual way (python hello_bad1.py).

Analysis#

You should see the following error message:

Traceback (most recent call last):
  File "/Someplace/Unique/To/You/Session_7/hello_bad1.py", line 1, in <module>
    datafile = open(greeting.txt)
                    ^^^^^^^^
NameError: name 'greeting' is not defined
  • The first line tells you this is a Traceback - a journey back through your code to find the source of the problem. The “most recent call last” part won’t mean much for now, it will be more relevant later on in the course when you use more functions and methods.

  • The next line identifies the file where the problem was found (the exact file name shown will depend on your own files and folders set-up).

  • The next line shows the culprit line of code, and underlines exactly where the problem is.

  • The last line defines the sort of error that it present in the code: in this case, a NameError.

So what is the NameError? Because you “forgot” to enclose the file name in quotes, Python assumed that greeting.txt refers to the .txt attrbute of an object with the name greeting. And there is no such object as greeting, hence the error message.

You can see that the Traceback has been very helpful in identifying exactly where the problem is, but it doesn’t tell you how to fix it. In a few cases Python will make some attempt to do this, but mostly this is as far as it goes and the rest of the detective work is up to you!


Task 2#

  1. Again, open hello.py in your favourite text editor, and deliberatly insert a different error. Find the line:

    datafile = open('greeting.txt')

    and change one of the quotes round the name of the file:

    datafile = open('greeting.txt")

  2. Save the edited file as ‘hello_bad2.py’ and try to run it.

Analysis#

This time you should get a message:

 File "/Someplace/Unique/To/You/Session_7/hello_bad2.py", line 1
    datafile = open('greeting.txt")
                    ^
SyntaxError: unterminated string literal (detected at line 1)

Notice the first line does not start Traceback. THis is because, as you can see from the last line of the message, this is a different type of error - a SyntaxError. A SyntaxError is an error that comes from writing “incorrect” Python - code that could never have run succesfully. Python didn’t have to attempt to actually run the code in order to find this mistake; because your code doesn’t conform to the “rules”, it’s picked up straight away. Because the code was never actually run, there is no Traceback.

But as before, the exact place in the exact line of code that’s causing the problem is identified. Python has seen the start of a string literal, marked with the single quote ('), but can’t find a matching single quote to terminate it, hence the error. Again, the source of the error is identified, but it requires detective work from you to work out how to fix it!


Task 3#

  1. Again, open hello.py in your favourite text editor, and deliberatly insert a different error. Find the line:

    print(message)

    and insert a deliberate typo:

    print(mesage)

  2. Save the edited file as ‘hello_bad3.py’ and try to run it.

Analysis#

This time you should get a message:

Traceback (most recent call last):
  File "/Someplace/Unique/To/You/Session_7/hello_bad6.py", line 4, in <module>
    print(mesage)
          ^^^^^^
NameError: name 'mesage' is not defined. Did you mean: 'message'?

Notice this is a Traceback again, and a NameError again. From the examples above, you will realise that this means that the code was theoretically correct Python, but in practice was not, and the error relates to mesage, which could be a legitimate name for an object, but no object with that name actually exists. But this time Python has gone the extra mile, and suggested what sort of mistake you might have made, and how you could correct it.


Task 4#

  1. Again, open hello.py in your favourite text editor, and deliberatly insert a different error. Find the line:

    message = datafile.read()

    and insert a deliberate typo:

    message = datafile.read

  2. Save the edited file as ‘hello_bad4.py’ and try to run it.

Analysis#

You should have got the output:

<built-in method read of _io.TextIOWrapper object at 0x10289d620>

(the “0x…” bit will probably be different). No mention of any error at all!

That’s because, from Python’s perspective, there is no error in this version of your code - the syntax is correct, and it can be executed without creating an error - it’s just that it’s not doing what you wanted it to do. The line:

message = datafile.read

is a valid assignment statement. It assigns message to be what datafile.read is: the read method of the datafile object. Those missing brackets () make all the difference between the name of a method, and the execution of a method. So in the line:

print(message)

that’s what gets printed.

This example illustrates something very important - that Python cannot always detect that there is an error in your program, if the “wrong” code is unfortunately still valid, executable, code. Sometimes it’s only by the coder’s detective work that the source of the problem can be worked out.


Programming Challenge

In this folder you will find a number of versions of a small python program age_test. There is a problem with each of them. Run each to see what error message is produced, then try to fix it!

Summary#

When you try to run code that contains errors, Python will do it’s best to let you know where the problem is, and what it is. If you are lucky, it might even be able to suggest a fix. But if your error still leaves you with a valid Python program, it will be much more up to you to work out why, although it’s working, it’s not doing what you wanted it to do.