Python-Ref > Basic usage > Debugging > Print statements in debugging
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Print statements in debugging

When a program does not behave in the way you would like it to, the most important task is to get detailed information about what it is actually doing. In 99% of cases you can use simple print statements for discover what is going on in your program.
The next example shows an attempt at a program that finds the position of a word 'weak' in a text.
Expand/Shrink
Zdroj: (debug3-1.py)
  1   """this program tries to find the position of the word 'weak' in a text"""
  2   
  3   import re
  4   
  5   # E.A.Poe, Raven, http://www.gutenberg.org/etext/14082
  6   text = '''Once upon a midnight dreary, while I pondered, weak and weary,
  7   Over many a quaint and curious volume of forgotten lore--
  8   While I nodded, nearly napping, suddenly there came a tapping,
  9   As of some one gently rapping--rapping at my chamber door.'''
 10   
 11   i = 0
 12   for word in re.findall( "\w", text):
 13     if word == "weak":
 14       print i
 15     i += 1
stdout:
Doba běhu: 22.8 ms
Strangely no output appears eventhough the word 'weak' is clearly in the text. To find out what is going on, we will insert a print statement to see what words were produced by the findall function.
Expand/Shrink
Zdroj: (debug3-2.py)
  1   """this program tries to find the position of the word 'weak' in a text"""
  2   
  3   import re
  4   
  5   # E.A.Poe, Raven, http://www.gutenberg.org/etext/14082
  6   text = '''Once upon a midnight dreary, while I pondered, weak and weary,
  7   Over many a quaint and curious volume of forgotten lore--
  8   While I nodded, nearly napping, suddenly there came a tapping,
  9   As of some one gently rapping--rapping at my chamber door.'''
 10   
 11   i = 0
 12   for word in re.findall( "\w", text):
 13     print word
 14     if word == "weak":
 15       print i
 16     i += 1
stdout:
O
n
c
e
u
p
o
n
a
m
i
d
n
i
g
h
t
d
r
e
a
r
y
w
h
i
l
e
I
p
o
n
d
e
r
e
d
w
e
a
k
a
n
d
w
e
a
r
y
O
v
e
r
m
a
n
y
a
q
u
a
i
n
t
a
n
d
c
u
r
i
o
u
s
v
o
l
u
m
e
o
f
f
o
r
g
o
t
t
e
n
l
o
r
e
W
h
i
l
e
I
n
o
d
d
e
d
n
e
a
r
l
y
n
a
p
p
i
n
g
s
u
d
d
e
n
l
y
t
h
e
r
e
c
a
m
e
a
t
a
p
p
i
n
g
A
s
o
f
s
o
m
e
o
n
e
g
e
n
t
l
y
r
a
p
p
i
n
g
r
a
p
p
i
n
g
a
t
m
y
c
h
a
m
b
e
r
d
o
o
r
Doba běhu: 23.3 ms
Now we clearly see where the problem is - the regexp does not return words but letters, we need to fix this.
Expand/Shrink
Zdroj: (debug3-3.py)
  1   """this program tries to find the position of the word 'weak' in a text"""
  2   
  3   import re
  4   
  5   # E.A.Poe, Raven, http://www.gutenberg.org/etext/14082
  6   text = '''Once upon a midnight dreary, while I pondered, weak and weary,
  7   Over many a quaint and curious volume of forgotten lore--
  8   While I nodded, nearly napping, suddenly there came a tapping,
  9   As of some one gently rapping--rapping at my chamber door.'''
 10   
 11   i = 0
 12   for word in re.findall( "\w+", text):
 13     print "*", word
 14     if word == "weak":
 15       print i
 16     i += 1
stdout:
* Once
* upon
* a
* midnight
* dreary
* while
* I
* pondered
* weak
8
* and
* weary
* Over
* many
* a
* quaint
* and
* curious
* volume
* of
* forgotten
* lore
* While
* I
* nodded
* nearly
* napping
* suddenly
* there
* came
* a
* tapping
* As
* of
* some
* one
* gently
* rapping
* rapping
* at
* my
* chamber
* door
Doba běhu: 22.8 ms
We have also included the "*" character before the debugging output to distinguish it from the real output.
When the program does what was expected, we can remove the debugging code and run it again.
Expand/Shrink
Zdroj: (debug3-4.py)
  1   """this program tries to find the position of the word 'weak' in a text"""
  2   
  3   import re
  4   
  5   # E.A.Poe, Raven, http://www.gutenberg.org/etext/14082
  6   text = '''Once upon a midnight dreary, while I pondered, weak and weary,
  7   Over many a quaint and curious volume of forgotten lore--
  8   While I nodded, nearly napping, suddenly there came a tapping,
  9   As of some one gently rapping--rapping at my chamber door.'''
 10   
 11   i = 0
 12   for word in re.findall( "\w+", text):
 13     if word == "weak":
 14       print i
 15     i += 1
stdout:
8
Doba běhu: 22.4 ms