Count to 10

Count to 10 next up previous contents
Next: Decisions Up: Non-Programmers Tutorial For Python Previous: Who Goes There?   Contents

Subsections

While loops

Presenting our first control structure. Ordinarily the computer starts with the first line and then goes down from there. Control structures change the order that statements are executed or decide if a certain statement will be run. Here's the source for a program that uses the while control structure:

a = 0
while a < 10:
        a = a + 1
        print a

And here is the extremely exciting output:

1
2
3
4
5
6
7
8
9
10

(And you thought it couldn't get any worse after turning your computer into a five dollar calculator?) So what does the program do? First it sees the line a = 0 and makes a zero. Then it sees while a < 10: and so the computer checks to see if a < 10. The first time the computer sees this statement a is zero so it is less than 10. In other words while a is less than ten the computer will run the tabbed in statements.

Here is another example of the use of while:

a = 1
s = 0
print 'Enter Numbers to add to the sum.'
print 'Enter 0 to quit.'
while a != 0 :
        print 'Current Sum:',s
        a = input('Number? ')
        s = s + a
print 'Total Sum =',s

The first time I ran this program Python printed out:

  File "sum.py", line 3
    while a != 0
                ^
SyntaxError: invalid syntax
I had forgotten to put the : after the while. The error message complained about that problem and pointed out where it thought the problem was with the SPMquot^" . After the problem was fixed here was what I did with the program:
Enter Numbers to add to the sum.
Enter 0 to quit.
Current Sum: 0
Number? 200
Current Sum: 200
Number? -15.25
Current Sum: 184.75
Number? -151.85
Current Sum: 32.9
Number? 10.00
Current Sum: 42.9
Number? 0
Total Sum = 42.9

Notice how print 'Total Sum =',s is only run at the end. The while statement only affects the line that are tabbed in (a.k.a. indented). The != means does not equal so while a != 0 : means until a is zero run the tabbed in statements that are afterwards.

Now that we have while loops, it is possible to have programs that run forever. An easy way to do this is to write a program like this:

while 1 == 1:
     print "Help, I'm stuck in a loop."

This program will output Help, I'm stuck in a loop. until the heat death of the universe or you stop it. The way to stop it is to hit the Control (or Ctrl) button and `c' (the letter) at the same time. This will kill the program. (Note: sometimes you will have to hit enter after the Control C.)

Examples

Fibonnacci.py

#This program calulates the fibonnacci sequence
a = 0
b = 1
count = 0
max_count = 20
while count < max_count:
    count = count + 1
    #we need to keep track of a since we change it
    old_a = a
    old_b = b
    a = old_b
    b = old_a + old_b
    #Notice that the , at the end of a print statement keeps it
    # from switching to a new line
    print old_a,
print

Output:

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

Password.py

# Waits until a password has been entered.  Use control-C to break out with out
# the password

#Note that this must not be the password so that the
# while loop runs at least once.
password = "foobar"

#note that != means not equal
while password != "unicorn":
    password = raw_input("Password:")
print "Welcome in"

Sample run:

Password:auo
Password:y22
Password:password
Password:open sesame
Password:unicorn
Welcome in


next up previous contents
Next: Decisions Up: Non-Programmers Tutorial For Python Previous: Who Goes There?   Contents
Josh Cogliati jjc@honors.montana.edu Wikibooks Version: Wikibooks Non-programmers Python Tutorial Contents