Debugging and asking for help with debugging

A place where you can post Python-related tutorials you made yourself, or links to tutorials made by others.

Debugging and asking for help with debugging

Postby Yoriz » Fri Feb 22, 2013 2:44 pm

Note This post was written by metulburr


Debugging

Debug = Identify and remove errors

Simple rules to follow when posting on forums with regards to debugging your code:
1) If your posting more than about 10 lines of code to illustrate your problem, then you are going to annoy people. Why? Because no one wants to review a hundred lines of code to find the problem. You are the one that created the code, so you know it best.

2) Isolate the problem. How? The Traceback error. It will indicate the name of the error and the line the error is found. If the error is within the definition of a function, then post that section of the function. Sometimes you may also have to post the function call also. And always post the entire traceback error.

3) When explaining the error, get to the point. Don't make an essay about it.

4) Google it. Copy and paste the last line of the traceback error into google. Many times people have had the same error you have had. Where many people have answered and solved it already.
Code: Select all
Traceback:
Traceback (most recent call last):
  File "forum.py", line 2, in <module>
    print(dicter.split())
AttributeError: 'dict' object has no attribute 'split'

This is a simple traceback error example.
1st line: Indicates that it is an error.
2nd line: shows the file or module the error is located in.
3rd line: is the exact line of code that caused the error.
4th line: Is the description of the error. It tells us it is an AttributeError, and that the object does not have that attribute.
Without even seeing the code the traceback error says it all in this case.

My line of code is valid, but I get a Traceback error for it?

Some Traceback errors are more complicated. The line of code that it shows as an error is valid. But you have to dig deeper. Think backwards. Let's take this code for example:
Code: Select all
lister = ['a','b']
lister.pop()
print(lister[1])


Each line of code here is valid, but the manner that they are all together are not.
1st line: creates a list with 2 indexes in it, the strings 'a', and 'b'
2nd line: pops the last index out of the list
3rd line: attempts to print the second index of the list 'b'
Now we get the traceback:
Code: Select all
Traceback (most recent call last):
  File "forum.py", line 3, in <module>
    print(lister[1])
IndexError: list index out of range


Now this does not mean that we typed the line of code print(lister[1]) wrong. It means that index of that list no longer exists. Even though we created that index, pop() took the last index of the list off and now we are trying to print an index of a list that no longer exists, giving us the error.
Even though this is simple, more complex problems can relate to this as the line of code that the error shows is not the direct problem.
The steps of finding this error:
1) Why is it not able to print?
2) What is it trying to print?
3) Any typo?
4) What makes the lists index out of range?
5) What affects that list previously of trying to print it?
Which will eventually lead you to the fact that you either need to not pop it, create a new index for it to print, or change the index of the list that it is trying to print.

Posting this on the forums
Now let's say you had this within a hundred lines of code, and you could not figure out the description of the traceback error. You would not post the entire hundred lines of code on the forums. Even as simple as it is, no one would look at it because no one would want to skim all those lines of code. You also don't want to just post the traceback and the line of the error that cause it, because then you are not giving enough information of what is affecting that list. In this situation posting the creation of the list, the line that affects the list, the print attempt of the list, and the traceback will give a small amount of code for the users to look at and find the answer to your problem, instead of them weaving through hundreds of lines of code to find it. Now you only posted a readble 7 lines of code. Post smart.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 842
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Return to Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests