Non-Programmer's Tutorial for Python 2.6/Count to 10

While loops
Here we present our first control structure. Ordinarily, the computer starts with the first line and then goes down from there. However, control structures change the order of how the statements are executed and/or decide if a certain statement(s) will be run. Here's the source for a program that uses the  control structure:

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  which tells the computer to sets   to the value of zero. Then, it sees  which tells the computer to check whether. The first time the computer sees this while statement,  is equal to zero, which means   is less than 10, so the computer proceeds to run the succeeding indented, or tabbed in, statements. After the last statement,, within this while "loop" is run, the computer goes back up again to the   to check the current value of. In other words, as long as  is less than ten, the computer will run the tabbed in statements. With  repeatedly adding one to , eventually the while loop makes   equal to ten, and makes the   no longer true. Reaching that point, the program will not run the indented lines any longer.

Always remember to put a colon ":" after the "while" statement!

Here is another example of the use of :

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  is only run at the end. The  statement only affects the lines that are indented with whitespace. The  means "does not equal" so   means: "until   is zero, run the tabbed statements that follow."

Infinite loops
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:

The "==" operator is used to test equality of the expressions on the two sides of the operator, just as "&lt;" was used for "less than" before (you will get a complete list of all comparison operators in the next chapter).

This program will output  until the heat death of the universe or until you stop it, because 1 will forever be equal to 1. 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
Fibonacci.py

Output:

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

Note the output on a single line by use of a comma at the end of the  statement.

Password.py

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

Exercises
Write a program that asks the user for a Login Name and password. Then when they type "lock", they need to type in their name and password to unlock the program.