The first documented bug was first found by Grace Hopper.
Steps
in Debugging (Tips and Techniques):
Describe
the Problem – If the problem is messy and it is not well understood in your
head, it is almost impossible to debug it. You have to untangle the problem and
try to make sense of what is going on.
Reproduce
the Bug – Bugs that show up occasionally are quite difficult to debug.
Reproducing the bug helps you discover the problem. (Not too understandable but
ok)
Remember
on day 2 when I said I was a computer sometimes?? The next technique is to
Play
Computer: The skill of pretending to be a computer, reading through your code
and imagining what you are going to do each time is really useful especially
when you are debugging.
When
the editor or console is giving you an error, fix them before you continue. On
the console, copy the error message that is not specific to your code and
google search adding python to it to be specific. Stack Overflow is a place
that offers good fix suggestions.
Try/Except
block is used to catch a potentially dangerous code and trap it in a try block,
then create an except block to counter it. Probably print out something to
inform the user of the situation.
Example
from the course:
try:
age = int(input("How old are you?"))
except ValueError:
print("You have typed
in a an invalid number. Please try again with a numerical response such as
15.")
age = int(input("How old are you?"))
if age
> 18:
print(f"You can drive
at age {age}.")
Try
to help people debug their code.
Use
the print() statement.
Use
a debugger in PyCharm
A
breakpoint is created by clicking on the gutter of our code (the numbers
located on the LHS i.e., the line numbers). The breakpoint puts a break on the
code at that particular line.
Take
a break
Ask
a friend
Run
your code often, confirm that it is actually doing what you want it to do
Ask
Stack Overflow
Final notes:
I was going to write out useful notes from today's debugging class but got tired at the later part of the class :(
But, I managed to break down tomorrow's exercise into workable comments.
:)
Comments
Post a Comment