?? node8.html
字號:
current line, it is the line right afterwards, so it is: <CODE>print "perimeter
= ",width+height+width+width</CODE> (It may also run a function in the current
line, but thats a future chapter.)
<P><B>Question: </B>What does that line do?
<P><B>Answer: </B>First it prints <TT>perimeter =</TT>, then it prints
<TT>width+height+width+width</TT>.
<P><B>Question: </B>Does <TT>width+height+width+width</TT> calculate the
perimeter properly?
<P><B>Answer: </B>Let's see, perimeter of a rectangle is the bottom (width) plus
the left side (height) plus the top (width) plus the right side (huh?). The last
item should be the right side's length, or the height.
<P><B>Question: </B>Do you understand why some of the times the perimeter was
calculated `correctly'?
<P><B>Answer: </B>It was calculated correctly when the width and the height were
equal.
<P>The next program we will do a code walkthrough for is a program that is
supposed to print out 5 dots on the screen. However, this is what the program is
outputting:
<P><PRE>. . . .
</PRE>
<P>And here is the program:
<P><PRE>number = 5
while number > 1:
print ".",
number = number - 1
print
</PRE>
<P>This program will be more complex to walkthrough since it now has indented
portions (or control structures). Let us begin.
<P><B>Question: </B>What is the first line to be run?
<P><B>Answer: </B>The first line of the file: <TT>number = 5</TT>
<P><B>Question: </B>What does it do?
<P><B>Answer: </B>Puts the number 5 in the variable number.
<P><B>Question: </B>What is the next line?
<P><B>Answer: </B>The next line is: <TT>while number > 1:</TT>
<P><B>Question: </B>What does it do?
<P><B>Answer: </B>Well, <TT>while</TT> statements in general look at their
expression, and if it is true they do the next indented block of code, otherwise
they skip the next indented block of code.
<P><B>Question: </B>So what does it do right now?
<P><B>Answer: </B>If <TT>number > 1</TT> is true then the next two lines will
be run.
<P><B>Question: </B>So is <TT>number > 1</TT>?
<P><B>Answer: </B>The last value put into <TT>number</TT> was <TT>5</TT> and
<TT>5 > 1</TT> so yes.
<P><B>Question: </B>So what is the next line?
<P><B>Answer: </B>Since the <TT>while</TT> was true the next line is:
<CODE>print ".",</CODE>
<P><B>Question: </B>What does that line do?
<P><B>Answer: </B>Prints one dot and since the statement ends with a <TT>,</TT>
the next print statement will not be on a different screen line.
<P><B>Question: </B>What is the next line?
<P><B>Answer: </B><CODE>number = number - 1</CODE> since that is following line
and there are no indent changes.
<P><B>Question: </B>What does it do?
<P><B>Answer: </B>It calculates <TT>number - 1</TT>, which is the current value
of <TT>number</TT> (or 5) subtracts 1 from it, and makes that the new value of
number. So basically it changes <TT>number</TT>'s value from 5 to 4.
<P><B>Question: </B>What is the next line?
<P><B>Answer: </B>Well, the indent level decreases so we have to look at what
type of control structure it is. It is a <TT>while</TT> loop, so we have to go
back to the <TT>while</TT> clause which is <CODE>while number > 1:</CODE>
<P><B>Question: </B>What does it do?
<P><B>Answer: </B>It looks at the value of number, which is 4, and compares it
to 1 and since <CODE>4 > 1</CODE> the while loop continues.
<P><B>Question: </B>What is the next line?
<P><B>Answer: </B>Since the while loop was true, the next line is: <CODE>print
".",</CODE>
<P><B>Question: </B>What does it do?
<P><B>Answer: </B>It prints a second dot on the line.
<P><B>Question: </B>What is the next line?
<P><B>Answer: </B>No indent change so it is: <CODE>number = number - 1</CODE>
<P><B>Question: </B>And what does it do?
<P><B>Answer: </B>It talks the current value of number (4), subtracts 1 from it,
which gives it 3 and then finally makes 3 the new value of number.
<P><B>Question: </B>What is the next line?
<P><B>Answer: </B>Since there is an indent change caused by the end of the while
loop, the next line is: <CODE>while number > 1:</CODE>
<P><B>Question: </B>What does it do?
<P><B>Answer: </B>It compares the current value of number (3) to 1. <CODE>3 >
1</CODE> so the while loop continues.
<P><B>Question: </B>What is the next line?
<P><B>Answer: </B>Since the while loop condition was true the next line is:
<CODE>print ".",</CODE>
<P><B>Question: </B>And it does what?
<P><B>Answer: </B>A third dot is printed on the line.
<P><B>Question: </B>What is the next line?
<P><B>Answer: </B>It is: <CODE>number = number - 1</CODE>
<P><B>Question: </B>What does it do?
<P><B>Answer: </B>It takes the current value of number (3) subtracts from it 1
and makes the 2 the new value of number.
<P><B>Question: </B>What is the next line?
<P><B>Answer: </B>Back up to the start of the while loop: <CODE>while number
> 1:</CODE>
<P><B>Question: </B>What does it do?
<P><B>Answer: </B>It compares the current value of number (2) to 1. Since
<CODE>2 > 1</CODE> the while loop continues.
<P><B>Question: </B>What is the next line?
<P><B>Answer: </B>Since the while loop is continuing: <CODE>print ".",</CODE>
<P><B>Question: </B>What does it do?
<P><B>Answer: </B>It discovers the meaning of life, the universe and everything.
I'm joking. (I had to make sure you were awake.) The line prints a fourth dot on
the screen.
<P><B>Question: </B>What is the next line?
<P><B>Answer: </B>It's: <CODE>number = number - 1</CODE>
<P><B>Question: </B>What does it do?
<P><B>Answer: </B>Takes the current value of number (2) subtracts 1 and makes 1
the new value of number.
<P><B>Question: </B>What is the next line?
<P><B>Answer: </B>Back up to the while loop: <CODE>while number > 1:</CODE>
<P><B>Question: </B>What does the line do?
<P><B>Answer: </B>It compares the current value of number (1) to 1. Since
<CODE>1 > 1</CODE> is false (one is not greater than one), the while loop
exits.
<P><B>Question: </B>What is the next line?
<P><B>Answer: </B>Since the while loop condition was false the next line is the
line after the while loop exits, or: <CODE>print</CODE>
<P><B>Question: </B>What does that line do?
<P><B>Answer: </B>Makes the screen go to the next line.
<P><B>Question: </B>Why doesn't the program print 5 dots?
<P><B>Answer: </B>The loop exits 1 dot too soon.
<P><B>Question: </B>How can we fix that?
<P><B>Answer: </B>Make the loop exit 1 dot later.
<P><B>Question: </B>And how do we do that?
<P><B>Answer: </B>There are several ways. One way would be to change the while
loop to: <CODE>while number > 0:</CODE> Another way would be to change the
conditional to: <CODE>number >= 1</CODE> There are a couple others.
<P>
<H1><A name=SECTION00840000000000000000>How do I fix the program?</A> </H1>
<P>You need to figure out what the program is doing. You need to figure out what
the program should do. Figure out what the difference between the two is.
Debugging is a skill that has to be done to be learned. If you can't figure it
out after an hour or so take a break, talk to someone about the problem or
contemplate the lint in your navel. Come back in a while and you will probably
have new ideas about the problem. Good luck.
<P>
<HR>
<!--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> <!--End of Navigation Panel-->
<ADDRESS>Josh Cogliati <A
href="mailto:jjc@honors.montana.edu">jjc@honors.montana.edu</A>
</ADDRESS></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -