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.
A code walkthrough starts at the first line, and works its way down until the program is done. While loops and if 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.
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:
height = input("Height: ") width = input("Width: ") print "perimeter = ",width+height+width+width
Question: What is the first line Python runs?
Answer: The first line is alway run first. In this case it is: height = input("Height: ")
Question: What does that line do?
Answer: Prints Height: , waits for the user to type a number in, and puts that in the variable height.
Question: What is the next line that runs?
Answer: In general, it is the next line down which is: width = input("Width: ")
Question: What does that line do?
Answer: Prints Width: , waits for the user to type a number in, and puts what the user types in the variable width.
Question: What is the next line that runs?
Answer: When the next line is not indented more or less than the current line,
it in the line right afterwards, so it is: print "perimeter = ",width+height+width+width
(It may also run a function in the current line, but thats a future chapter.)
Question: What does that line do?
Answer: First it prints perimeter =, then it prints width+height+width+width.
Question: Does width+height+width+width calculate the perimeter properly?
Answer: 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.
Question: Do you understand why some of the times the perimeter was calculated `correctly'?
Answer: It was calculated correctly when the width and the height were equal.
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:
. . . .
And here is the program:
number = 5 while number > 1: print ".", number = number - 1 print
This program will be more complex to walkthrough since it now has indented portions (or control structures). Let us begin.
Question: What is the first line to be run?
Answer: The first line of the file: number = 5
Question: What does it do?
Answer: Puts the number 5 in the variable number.
Question: What is the next line?
Answer: The next line is: while number > 1:
Question: What does it do?
Answer: Well, while 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.
Question: So what does it do right now?
Answer: If number > 1 is true then the next two lines will be run.
Question: So is number > 1?
Answer: The last value put into number was 5 and 5 > 1 so yes.
Question: So what is the next line?
Answer: Since the while was true the next line is: print ".",
Question: What does that line do?
Answer: Prints one dot and since the statement ends with a , the next print statement will not be on a different screen line.
Question: What is the next line?
Answer: number = number - 1
since that is following line and there are no indent changes.
Question: What does it do?
Answer: It calculates number - 1, which is the current value of number (or 5) subtracts 1 from it, and makes that the new value of number. So basically it changes number's value from 5 to 4.
Question: What is the next line?
Answer: Well, the indent level decreases so we have to look at what type of control structure it is. It is a while loop, so we have to go back to the while clause which is while number > 1:
Question: What does it do?
Answer: It looks at the value of number, which is 4, and compares it to 1 and since 4 > 1
the while loop continues.
Question: What is the next line?
Answer: Since the while loop was true, the next line is: print ".",
Question: What does it do?
Answer: It prints a second dot on the line.
Question: What is the next line?
Answer: No indent change so it is: number = number - 1
Question: And what does it do?
Answer: 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.
Question: What is the next line?
Answer: Since there is an indent change caused by the end of the while loop, the next line is: while number > 1:
Question: What does it do?
Answer: It compares the current value of number (3) to 1. 3 > 1
so the while loop continues.
Question: What is the next line?
Answer: Since the while loop condition was true the next line is: print ".",
Question: And it does what?
Answer: A third dot is printed on the line.
Question: What is the next line?
Answer: It is: number = number - 1
Question: What does it do?
Answer: It takes the current value of number (3) subtracts from it 1 and makes the 2 the new value of number.
Question: What is the next line?
Answer: Back up to the start of the while loop: while number > 1:
Question: What does it do?
Answer: It compares the current value of number (2) to 1. Since 2 > 1
the while loop continues.
Question: What is the next line?
Answer: Since the while loop is continuing: print ".",
Question: What does it do?
Answer: 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.
Question: What is the next line?
Answer: It's: number = number - 1
Question: What does it do?
Answer: Takes the current value of number (2) subtracts 1 and makes 1 the new value of number.
Question: What is the next line?
Answer: Back up to the while loop: while number > 1:
Question: What does the line do?
Answer: It compares the current value of number (1) to 1. Since 1 > 1
is false (one is not greater than one), the while loop exits.
Question: What is the next line?
Answer: Since the while loop condition was false the next line is the line after the while loop exits, or: print
Question: What does that line do?
Answer: Makes the screen goto the next line.
Question: Why doesn't the program print 5 dots?
Answer: The loop exits 1 dot too soon.
Question: How can we fix that?
Answer: Make the loop exit 1 dot later.
Question: And how do we do that?
Answer: There are several ways. One way would be to change the while loop to:
while number > 0:
Another way would be to change the conditional to: number >= 1
There are a couple others.