# 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 while control structure:
And here is the extremely exciting output:
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 which tells the computer to sets a to the value of zero. Then, it sees while a < 10: which tells the computer to check whether a < 10. The first time the computer sees this while statement, a is equal to zero, which means a is less than 10, so the computer proceeds to run the succeeding indented, or tabbed in, statements. After the last statement, print (a), within this while "loop" is run, the computer goes back up again to the while a < 10 to check the current value of a. In other words, as long as a is less than ten, the computer will run the tabbed in statements. With a = a + 1 repeatedly adding one to a, eventually the while loop makes a equal to ten, and makes the a < 10 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 while:
Notice how print 'Total Sum =', s is only run at the end. The while statement only affects the lines that are indented with whitespace. The != means "does not equal" so "while a != 0:" means: "until a 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 "<" was used for "less than" before (you will get a complete list of all comparison operators in the next chapter).
This program will output Help, I'm stuck in a loop. 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:
Note the output on a single line by use of a comma at the end of the print statement.
Password.py
Sample run:

### 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.
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.
If you would like the program to run continuously, just add a while 1 == 1: loop around the whole thing. You will have to indent the rest of the program when you add this at the top of the code, but don't worry, you don't have to do it manually for each line! Just highlight everything you want to indent and click on "Indent" under "Format" in the top bar of the python window. Note that you can use empty strings like this: "".
Another way of doing this could be:
Notice the or in while (name != "user") or (password != "pass"):, which we haven't yet introduced. You can probably figure out how it works.
This method, although a bit more crude also works. Notice it uses the as of yet un-introduced if function.
| Previous: Who Goes There? | Front Page | Next: Decisions |