Once you come up with an idea for a code, it is almost never the case that your first implementation will work.
When this happens, you need to take a breath and start debugging.
Debugging literally comes from de-bugging which means cleaning the bugs, flaws, errors, issues related to code and software.
Debugging is basically tracing your program in your head (often with the help of some pen and paper) for some particular input, and finding out where and why it fails. Obviously you should choose an input that makes your program fail, but something small enough so that you don't need to run too many steps until failing. Your input should be as simple as possible and as complicated as necessary.
If you happen to have a computer around, you can use it to help you trace the code by adding print
statements to your code.
Although it can be said that debugging would be best learned through real life practice of fixing errors, there are some basic knowledge and methods that can raise your awareness, save you time and make you a much more efficient debugger as well as coder. Debugging / Testing is a huge topic in software development but as a beginner-intermediate programmer one usually needs much more primitive debugging skills and knowledge. Once testing has uncovered problems, the next step is to fix them. Many novices do this by making more-or-less random changes to their code until it seems to produce the right answer, but that’s very inefficient (and the result is usually only correct for the one case they’re testing). The more experienced a programmer is, the more systematically they debug, and most follow some variation on the rules explained below.
print("Hello world!")
Hello world!
print
is a python command used to print things to the console.
Whatever is in the parentheses is going to be printed. If the thing in parentheses is in quotes, it is going to be printed ipsis litteris (word per word). If the thing in parentheses is a variable, python prints the value that is inside that variable.
x = 10
print(x)
10
Quotes and variables can be combined inside prints to make the messages more understandable. A comma indicates there is a space between the parts.
x = 42
print("The answer to the life, universe, and everything is", x, ".")
The answer to the life, universe, and everything is 42 .
Note that this does not alter the flow of your program. Python does nothing to whatever is printed, and this is only for your scrutinity.
You can use print
to find out how many times a loop runs, and what is the value of the loop variable.
def f(n):
for i in range(n):
print("i is", i)
f(3)
i is 0 i is 1 i is 2
def f(n):
for i in range(1,n):
print("i is", i)
f(3)
i is 1 i is 2
def f(n):
for i in range(1, n + 1):
print("i is", i)
f(3)
i is 1 i is 2 i is 3
print
s are particularly useful if there are loops inside loops.
def f(n, m):
for i in range(n):
print(i, ": ", end="") # end="" avoids a line break after print
for j in range(m):
print(j, end=", ") # end=", " places a comma after every print
print()
f(4, 3)
0 : 0, 1, 2, 1 : 0, 1, 2, 2 : 0, 1, 2, 3 : 0, 1, 2,
def f(n):
for i in range(n):
print(i, ": ", end="")
# Range for the second loop depends on first loop variable
for j in range(i):
print(j, end=", ")
print()
f(10)
0 : 1 : 0, 2 : 0, 1, 3 : 0, 1, 2, 4 : 0, 1, 2, 3, 5 : 0, 1, 2, 3, 4, 6 : 0, 1, 2, 3, 4, 5, 7 : 0, 1, 2, 3, 4, 5, 6, 8 : 0, 1, 2, 3, 4, 5, 6, 7, 9 : 0, 1, 2, 3, 4, 5, 6, 7, 8,
Add print statements to trace the code and spot the "bugs"
def numberOfPoolBalls(rows):
n = 0
sum = 0
if rows > 0:
for i in range(0, rows):
n = n + 1
sum = sum + n
return sum
else:
return 0
def numberOfPoolBalls(rows):
n = 0
sum = 0
if rows > 0:
for i in range(0, rows):
n = n + 1
sum = sum + n
return sum
else:
return 0
print(numberOfPoolBalls(4))
1
print
statements to your code.def numberOfPoolBalls(rows):
n = 0
sum = 0
if rows > 0:
for i in range(0, rows):
n = n + 1
sum = sum + n
print("i =", i, " n=", n, "sum=", sum)
return sum
else:
return 0
print(numberOfPoolBalls(4))
i = 0 n= 1 sum= 1 1
return sum
outside the loopdef numberOfPoolBalls(rows):
n = 0
sum = 0
if rows > 0:
for i in range(0, rows):
n = n + 1
sum = sum + n
print("i =", i, " n=", n, "sum=", sum)
return sum
else:
return 0
print(numberOfPoolBalls(4))
i = 0 n= 1 sum= 1 i = 1 n= 2 sum= 3 i = 2 n= 3 sum= 6 i = 3 n= 4 sum= 10 10
5. Test more
assert(numberOfPoolBalls(4) == 10)
assert(numberOfPoolBalls(4) == 10)
assert(numberOfPoolBalls(4) == 10)
assert(numberOfPoolBalls(4) == 10)
def toBinary(n):
binary = 0
cnt = 0
while n >= 0:
bit = n%2
n = n / 2
binary = bit*(10**cnt) + binary
cnt = cnt + 1
return binary
assert(toBinary(8) == 1000)
--------------------------------------------------------------------------- OverflowError Traceback (most recent call last) <ipython-input-29-cb6b9432c818> in <module> 9 return binary 10 ---> 11 assert(toBinary(8) == 1000) <ipython-input-29-cb6b9432c818> in toBinary(n) 5 bit = n%2 6 n = n / 2 ----> 7 binary = bit*(10**cnt) + binary 8 cnt = cnt + 1 9 return binary OverflowError: int too large to convert to float
print
statements to your code.def toBinary(n):
binary = 0
cnt = 0
while n >= 0:
bit = n%2
n = n / 2
binary = bit*(10**cnt) + binary
cnt = cnt + 1
print("bit =", bit, "n=", n, "binary=", binary, "cnt=", cnt)
return binary
print(toBinary(8))
bit = 0 n= 4.0 binary= 0 cnt= 1 bit = 0.0 n= 2.0 binary= 0.0 cnt= 2 bit = 0.0 n= 1.0 binary= 0.0 cnt= 3 bit = 1.0 n= 0.5 binary= 1000.0 cnt= 4 bit = 0.5 n= 0.25 binary= 6000.0 cnt= 5 bit = 0.25 n= 0.125 binary= 31000.0 cnt= 6 bit = 0.125 n= 0.0625 binary= 156000.0 cnt= 7 bit = 0.0625 n= 0.03125 binary= 781000.0 cnt= 8 bit = 0.03125 n= 0.015625 binary= 3906000.0 cnt= 9 bit = 0.015625 n= 0.0078125 binary= 19531000.0 cnt= 10 bit = 0.0078125 n= 0.00390625 binary= 97656000.0 cnt= 11 bit = 0.00390625 n= 0.001953125 binary= 488281000.0 cnt= 12 bit = 0.001953125 n= 0.0009765625 binary= 2441406000.0 cnt= 13 bit = 0.0009765625 n= 0.00048828125 binary= 12207031000.0 cnt= 14 bit = 0.00048828125 n= 0.000244140625 binary= 61035156000.0 cnt= 15 bit = 0.000244140625 n= 0.0001220703125 binary= 305175781000.0 cnt= 16 bit = 0.0001220703125 n= 6.103515625e-05 binary= 1525878906000.0 cnt= 17 bit = 6.103515625e-05 n= 3.0517578125e-05 binary= 7629394531000.0 cnt= 18 bit = 3.0517578125e-05 n= 1.52587890625e-05 binary= 38146972656000.0 cnt= 19 bit = 1.52587890625e-05 n= 7.62939453125e-06 binary= 190734863281000.0 cnt= 20 bit = 7.62939453125e-06 n= 3.814697265625e-06 binary= 953674316406000.0 cnt= 21 ...
def toBinary(n):
binary = 0
cnt = 0
while n >= 0:
bit = n%2
n = n // 2
binary = bit*(10**cnt) + binary
cnt = cnt + 1
print("bit =", bit, "n=", n, "binary=", binary, "cnt=", cnt)
return binary
print(toBinary(8))
bit = 0 n= 4 binary= 0 cnt= 1 bit = 0 n= 2 binary= 0 cnt= 2 bit = 0 n= 1 binary= 0 cnt= 3 bit = 1 n= 0 binary= 1000 cnt= 4 bit = 0 n= 0 binary= 1000 cnt= 5 bit = 0 n= 0 binary= 1000 cnt= 6 bit = 0 n= 0 binary= 1000 cnt= 7 bit = 0 n= 0 binary= 1000 cnt= 8 bit = 0 n= 0 binary= 1000 cnt= 9 bit = 0 n= 0 binary= 1000 cnt= 10 bit = 0 n= 0 binary= 1000 cnt= 11 bit = 0 n= 0 binary= 1000 cnt= 12 bit = 0 n= 0 binary= 1000 cnt= 13 bit = 0 n= 0 binary= 1000 cnt= 14 bit = 0 n= 0 binary= 1000 cnt= 15 bit = 0 n= 0 binary= 1000 cnt= 16 bit = 0 n= 0 binary= 1000 cnt= 17 bit = 0 n= 0 binary= 1000 cnt= 18 bit = 0 n= 0 binary= 1000 cnt= 19 bit = 0 n= 0 binary= 1000 cnt= 20 bit = 0 n= 0 binary= 1000 cnt= 21 ...
def toBinary(n):
binary = 0
cnt = 0
while n > 0:
bit = n%2
n = n // 2
binary = bit*(10**cnt) + binary
cnt = cnt + 1
print("bit =", bit, "n=", n, "binary=", binary, "cnt=", cnt)
return binary
print(toBinary(8))
bit = 0 n= 4 binary= 0 cnt= 1 bit = 0 n= 2 binary= 0 cnt= 2 bit = 0 n= 1 binary= 0 cnt= 3 bit = 1 n= 0 binary= 1000 cnt= 4 1000
def isPalindromeNumber(n):
# count the number of digits
ndigits = 0
while n >= 0:
n = n // 10
ndigits + 1
for i in range(ndigits):
# compare digit i with digit ndigits - i
if (n // 10 ** i) % 10 == (n // 10 ** (ndigits - i)) % 10:
return True
return False