Learning Python 3 with the Linkbot/Count to 10

Lesson Information **To Be Added** Vocabulary: Necessary Materials and Resources: Computer Science Teachers Association Standards: 5.2.CPP.5: Implement problem solutions using a programming language, including: looping behavior, conditional statements, logic, expressions, variables, and functions. Common Core Math Content Standards: Common Core Math Practice Standards: Common Core English Language Arts Standards:

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:

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  and sets   to zero. Then it sees  and so the computer checks to see if. The first time the computer sees this statement,  is zero, so it is less than 10. In other words, as long as  is less than ten, the computer will run the tabbed in statements. This eventually makes  equal to ten (by adding one to   again and again) and the   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  statement line!

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.0 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 as long as   is not zero run the tabbed statements that follow.

Note that  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 "&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 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!

Fibonacci sequence
Fibonacci-method1.py

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

Note that the output is on a single line because of the extra argument  in the   arguments.

Fibonacci-method2.py

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

Enter password
Password.py

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

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.

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.

Modify the Linkbot Buzzer program to play every third key on the piano from the 34th key up to the 74th key.