?? node8.html
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0061)http://www.honors.montana.edu/~jjc/easytut/easytut/node8.html -->
<!--Converted with LaTeX2HTML 99.2beta6 (1.42)original version by: Nikos Drakos, CBLU, University of Leeds* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>Debugging</TITLE>
<META content=Debugging name=description>
<META content=easytut name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<META content=text/css http-equiv=Content-Style-Type><LINK
href="node8_files/easytut.css" rel=STYLESHEET><LINK href="node9.html"
rel=next><LINK href="node7.html" rel=previous><LINK href="easytut.html"
rel=up><LINK href="node9.html" rel=next></HEAD>
<BODY><!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node9.html"
name=tex2html243><IMG align=bottom alt=next border=0 height=24
src="node8_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html239><IMG align=bottom alt=up border=0 height=24
src="node8_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node7.html"
name=tex2html233><IMG align=bottom alt=previous border=0 height=24
src="node8_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html241><IMG align=bottom alt=contents border=0 height=24
src="node8_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node9.html"
name=tex2html244>Defining Functions</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html240>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node7.html"
name=tex2html234>Decisions</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html242>Contents</A></B> <BR><BR><!--End of Navigation Panel--><!--Table of Child-Links--><A
name=CHILD_LINKS><STRONG>Subsections</STRONG></A>
<UL>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node8.html#SECTION00810000000000000000"
name=tex2html245>What is debugging?</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node8.html#SECTION00820000000000000000"
name=tex2html246>What should the program do?</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node8.html#SECTION00830000000000000000"
name=tex2html247>What does the program do?</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node8.html#SECTION00840000000000000000"
name=tex2html248>How do I fix the program?</A> </LI></UL><!--End of Table of Child-Links-->
<HR>
<H1><A name=SECTION00800000000000000000>Debugging</A> </H1>
<H1><A name=SECTION00810000000000000000>What is debugging?</A> </H1>
<BLOCKQUOTE>As soon as we started programming, we found to our surprise that
it wasn't as easy to get programs right as we had thought. Debugging had to be
discovered. I can remember the exact instant when I realized that a large part
of my life from then on was going to be spent in finding mistakes in my own
programs. </BLOCKQUOTE>
<P>
<BLOCKQUOTE>- Maurice Wilkes discovers debugging, 1949 </BLOCKQUOTE>
<P>By now if you have been messing around with the programs you have probably
found that sometimes the program does something you didn't want it to do. This
is fairly common. Debugging is the process of figuring out what the computer is
doing and then getting it to do what you want it to do. This can be tricky. I
once spent nearly a week tracking down and fixing a bug that was caused by
someone putting an <TT>x</TT> where a <TT>y</TT> should have been.
<P>This chapter will be more abstract than previous chapters. Please tell me if
it is useful.
<P>
<H1><A name=SECTION00820000000000000000>What should the program do?</A> </H1>
<P>The first thing to do (this sounds obvious) is to figure out what the program
should be doing if it is running correctly. Come up with some test cases and see
what happens. For example, let's say I have a program to compute the perimeter
of a rectangle (the sum of the length of all the edges). I have the following
test cases:
<P>
<TABLE border=1 cellPadding=3>
<TBODY>
<TR>
<TD align=left>width</TD>
<TD align=left>height</TD>
<TD align=left>perimeter</TD></TR>
<TR>
<TD align=left>3</TD>
<TD align=left>4</TD>
<TD align=left>14</TD></TR>
<TR>
<TD align=left>2</TD>
<TD align=left>3</TD>
<TD align=left>10</TD></TR>
<TR>
<TD align=left>4</TD>
<TD align=left>4</TD>
<TD align=left>16</TD></TR>
<TR>
<TD align=left>2</TD>
<TD align=left>2</TD>
<TD align=left>8</TD></TR>
<TR>
<TD align=left>5</TD>
<TD align=left>1</TD>
<TD align=left>12</TD></TR></TBODY></TABLE>
<P>I now run my program on all of the test cases and see if the program does
what I expect it to do. If it doesn't then I need to find out what the computer
is doing.
<P>More commonly some of the test cases will work and some will not. If that is
the case you should try and figure out what the working ones have in common. For
example here is the output for a perimeter program (you get to see the code in a
minute):
<P><PRE>Height: 3
Width: 4
perimeter = 15
</PRE><PRE>Height: 2
Width: 3
perimeter = 11
</PRE><PRE>Height: 4
Width: 4
perimeter = 16
</PRE><PRE>Height: 2
Width: 2
perimeter = 8
</PRE><PRE>Height: 5
Width: 1
perimeter = 8
</PRE>
<P>Notice that it didn't work for the first two inputs, it worked for the next
two and it didn't work on the last one. Try and figure out what is in common
with the working ones. Once you have some idea what the problem is finding the
cause is easier. With your own programs you should try more test cases if you
need them.
<P>
<H1><A name=SECTION00830000000000000000>What does the program do?</A> </H1>
<P>The next thing to do is to look at the source code. One of the most important
things to do while programming is reading source code. The primary way to do
this is code walkthroughs.
<P>A code walkthrough starts at the first line, and works its way down until the
program is done. <TT>While</TT> loops and <TT>if</TT> statements mean that some
lines may never be run and some lines are run many times. At each line you
figure out what Python has done.
<P>Lets start with the simple perimeter program. Don't type it in, you are going
to read it, not run it. The source code is:
<P><PRE>height = input("Height: ")
width = input("Width: ")
print "perimeter = ",width+height+width+width
</PRE>
<P><B>Question: </B>What is the first line Python runs?
<P><B>Answer: </B>The first line is alway run first. In this case it is:
<CODE>height = input("Height: ")</CODE>
<P><B>Question: </B>What does that line do?
<P><B>Answer: </B>Prints <TT>Height: </TT>, waits for the user to type a number
in, and puts that in the variable height.
<P><B>Question: </B>What is the next line that runs?
<P><B>Answer: </B>In general, it is the next line down which is: <CODE>width =
input("Width: ")</CODE>
<P><B>Question: </B>What does that line do?
<P><B>Answer: </B>Prints <TT>Width: </TT>, waits for the user to type a number
in, and puts what the user types in the variable width.
<P><B>Question: </B>What is the next line that runs?
<P><B>Answer: </B>When the next line is not indented more or less than the
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -