3. Debugging

To be a Successful Programmer


Debugging is one of the most important skills
Secret To Success
Get something working and keep it working

Avoid debugging


Get something working and keep it working
  1. Start Small

  2. Keep it working

An example


Alarm clock problem in general

Ask the user for the time now (in hours), and ask for the number of hours to wait. Your program should output what the time will be on the clock when the alarm goes off.

In [ ]:
current_time = input("what is the current time (in hours)?")
wait_time = input("How many hours do you want to wait")

print(current_time)
print(wait_time)

Next to figure out what the time will be after waiting wait_time number of hours

In [ ]:
current_time = input("What is the current time (in hours 0 - 23)?")
wait_time = input("How many hours do you want to wait")

print(current_time)
print(wait_time)

final_time = current_time + wait_time
print(final_time)

The problem:

Python is doing string concatenation, not integer addition

In [ ]:
current_time_str = input("What is the current time (in hours 0-23)?")
wait_time_str = input("How many hours do you want to wait")

current_time_int = int(current_time_str)
wait_time_int = int(wait_time_str)

final_time_int = current_time_int + wait_time_int
print(final_time_int)

Testing on boundary conditions

current_time_int = 10

wait_time_int = 20

we get final_time_int of 30

In [ ]:
current_time_str = input("What is the current time (in hours 0-23)?")
wait_time_str = input("How many hours do you want to wait")

current_time_int = int(current_time_str)
wait_time_int = int(wait_time_str)

final_time_int = current_time_int + wait_time_int

final_answer = final_time_int % 24

print("The time after waiting is: ", final_answer)

Beginning tips for Debugging


The process of debugging is much more like being a detective.

  • Everyone is a suspect (Except Python)!

  • Find clues.

    • Error Messages

    • Print Statements

Know your error messages

In [ ]:
%reset -f
current_time_str = input("What is the current time (in hours 0-23)?")
wait_time_str = input("How many hours do you want to wait")

current_time_int = int(current_time_str)
wait_time_int = int(wait_time_int)

final_time_int = current_time_int + wait_time_int
print(final_time_int)

Statistics about errors

Message Number Percent
ParseError 4999 54.74%
TypeError 1305 14.29%
NameError 1009 11.05%
ValueError 893 9.78%

ParseError

Parse errors happen when you make an error in the syntax of your program.

Usually ParseErrors can be traced back to missing punctuation characters, such as parenthesis, quotation marks, or commas.

Paretheses must be balanced.

In [ ]:
current_time_str = input("What is the current time (in hours 0-23)?")
wait_time_str = input("How many hours do you want to wait"

current_time_int = int(current_time_str)
wait_time_int = int(wait_time_str)

final_time_int = current_time_int + wait_time_int
print(final_time_int)

TypeError

TypeErrors occur when you you try to combine two objects that are not compatible.

For example you try to add together an integer and a string.

Usually type errors can be isolated to lines that are using mathematical operators, and usually the line number given by the error message is an accurate indication of the line.

In [2]:
a = input(u'wpisz godzine')
x = input(u'wpisz liczbe godzin')
int(x)
int(a)
h = x // 24
s = x % 24
print (h, s)
a = a + s
print ('godzina teraz %s' %a)
wpisz godzine1
wpisz liczbe godzin2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-6a5dac247c92> in <module>
      3 int(x)
      4 int(a)
----> 5 h = x // 24
      6 s = x % 24
      7 print (h, s)

TypeError: unsupported operand type(s) for //: 'str' and 'int'

NameError

Name errors almost always mean that you have used a variable before it has a value.

Often NameErrors are simply caused by typos in your code.

In [2]:
str_time = input("What time is it now?")
str_wait_time = input("What is the number of nours to wait?")
time = int(str_time)
wai_time = int(str_wait_time)

time_when_alarm_go_off = time + wait_time
print(time_when_alarm_go_off)
What time is it now?3
What is the number of nours to wait?4
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-8ed215e464ca> in <module>
      4 wai_time = int(str_wait_time)
      5 
----> 6 time_when_alarm_go_off = time + wait_time
      7 print(time_when_alarm_go_off)

NameError: name 'wait_time' is not defined

Another example

In [1]:
n = input("What time is it now (in hours)?")
n = imt(n)
m = input("How many hours do you want to wait?")
m = int(m)
q = m % 12
print("The time is now", q)
What time is it now (in hours)?8
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-2d54a1643bdb> in <module>
      1 n = input("What time is it now (in hours)?")
----> 2 n = imt(n)
      3 m = input("How many hours do you want to wait?")
      4 m = int(m)
      5 q = m % 12

NameError: name 'imt' is not defined

And another

In [3]:
present_time = input("Enter the present timein hours:")
set_alarm = input("Set the hours for alarm:")
int (present_time, set_time, alarm_time)
alarm_time = present_time + set_alarm
print(alarm_time)
Enter the present timein hours:10
Set the hours for alarm:20
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-85f0db5990a6> in <module>
      1 present_time = input("Enter the present timein hours:")
      2 set_alarm = input("Set the hours for alarm:")
----> 3 int (present_time, set_time, alarm_time)
      4 alarm_time = present_time + set_alarm
      5 print(alarm_time)

NameError: name 'set_time' is not defined

ValueError

Value errors occur when you pass a parameter to a function and the function is expecting a certain type, but you pass it a different type.

Test different inputs running the following example

In [4]:
current_time_str = input("What is the current time (in hours 0-23)?")
current_time_int = int(current_time_str)

wait_time_str = input("How many hours do you want to wait")
wait_time_int = int(wait_time_int)

final_time_int = current_time_int + wait_time_int
print(final_time_int)
What is the current time (in hours 0-23)?11
How many hours do you want to wait45
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-c63a31c8b65d> in <module>
      3 
      4 wait_time_str = input("How many hours do you want to wait")
----> 5 wait_time_int = int(wait_time_int)
      6 
      7 final_time_int = current_time_int + wait_time_int

NameError: name 'wait_time_int' is not defined

Suggestion

You need to keep track of the types of your variables, and understand what types your function is expecting.

You can do this by writing comments in your code, or by naming your variables in a way that reminds you of their type.

print statements are your friends.