# Learning Python 3 with the Linkbot/Count to 10

Lesson Information

### While loops

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:
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 and sets a to 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, as long as a is less than ten, the computer will run the tabbed in statements. This eventually makes a equal to ten (by adding one to a again and again) and the while a < 10 is not true any longer. Reaching that point, the program will stop running the indented lines.
Always remember to put a colon ":" at the end of the while statement line!
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 as long as a is not zero run the tabbed statements that follow.
Note that a is a floating point number, and not all floating point numbers can be accurately represented, so using != on them can sometimes not work. Try typing in 1.1 in interactive mode.

#### Infinite loops or Never Ending Loop

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 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.) On some systems, nothing will stop it, short of killing the process--so avoid!

### Examples
#### Fibonacci sequence

Fibonacci-method1.py
Output:
Note that the output is on a single line because of the extra argument end=" " in the print arguments.
Fibonacci-method2.py
Output:

#### Enter password

Password.py
Sample run:

#### A Linkbot Musical Example

The Linkbot has a small buzzer on board that can play one note at a time. We can control the frequency of the buzzer on the Linkbot to generate different tones. The equation $\mathrm {pianokeyFrequency} =2^{((\mathrm {key} -49)/12)}*440$ shows how to calculate the frequency of a key on a piano. There are 88 keys on a real piano, but lets see if we can make our robot play from the 34th key all the way up to the 73rd key.
In Python, to calculate the "power" of a number, we can use the ** operator. For instance, $2^{3}$ can be calculated in Python using 2**3.

### 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.
Another way of doing this could be:
Notice the or in while (nameguess != name) or (passwordguess != password), which we haven't yet introduced. You can probably figure out how it works.
Modify the Linkbot Buzzer program to play every third key on the piano from the 34th key up to the 74th key.