kids · lessons · programming · python · raspberry pi

Lessons on PI – Lesson 2(cont.) A little programming…

programming-languages

What is programming?

Programming is the process of recording knowledge or tasks into code.  Code like Python, or Javascript is a bridge “language” that allows humans to write their intent into formulas that a computer can then use like a recipe for action.

Computers don’t get bored…

In the previous lesson we used git to get some pre-made code from the internet.  This is generally how a programmer gets started.  First finding exemplary programs, pulling them down, understanding the, then extending them to do what is new.  So lets make sure we have the kidpi code:

git clone https://github.com/mamacker/kidpi.git

That creates a directory called kidpi.  Lets take a look at that directory again:

cd kidpi
ls

You should see a listing like:

PaPiRus  apmagic  grove  nodegpio  README.md forever  lirc  python

Lets “change directory” to python:

cd python

Ok, lets see what programs are there:

ls

Try the program you saw there before:

python count.py

If everything went well you should see a whole lotta:

.
.
.
To infinity and beyond! 875042!
To infinity and beyond! 875043!
To infinity and beyond! 875044!
To infinity and beyond! 875045!
To infinity and beyond! 875046!
To infinity and beyond! 875047!
To infinity and beyond! 875048!
To infinity and beyond! 875049!
To infinity and beyond! 875050!
To infinity and beyond! 875051!
To infinity and beyond! 875052!
To infinity and beyond! 875053!
To infinity and beyond! 875054!
To infinity and beyond! 875055!
To infinity and beyond! 875056!
To infinity and beyond! 875057!
To infinity and beyond! 875058!
To infinity and beyond! 875059!
To infinity and beyond! 875060!
To infinity and beyond! 875061!
.
.
.

Editing the program…

If so great!  You just ran the counting program.  Now lets try doing some programming.  Edit the file using VIM:

vim count.py

VIM stands for VI improved and it is one of the prominent editors used in programming.  When you ran that command you started editing the file and should see something like:

Screen Shot 2017-05-08 at 10.26.17 AMIf you look at that code – you can start to get a sense for how it instructed the computer to count.  The computer for its part will attempt to run the code as fast as it possibly can.  Which turns out to be pretty fast!

A little change…

Lets now change it a little, and try running the program after your changes.  To move around in VIM, use the arrow keys until you get the cursor to the word infinity.  Now hit the  key on the keyboard.  This will delete a character.  Hit it a couple more times until the word infinity is completely gone.  Now hit the i key.  This will put VIM in insert mode.  Now type in a new word.  Maybe “mars” like this:

Screen Shot 2017-05-08 at 10.35.27 AM

Now save out that edit by hitting esc which is the key on the upper left of the keyboard.  That tells VIM to get out of “insert” mode, and back into command mode.  Give VIM the command to write out the file and quit – its actually three key strokes “:” for command, “w” for write, and “q” for quit like this:

:wq

Running your program

Which brings you back to the prompt.  You’ve just programmed!  Lets try out your new program:

python count.py

Now it should start counting like mad again… but this time it says:

.
.
.
To mars and beyond! 572788!
To mars and beyond! 572789!
To mars and beyond! 572790!
To mars and beyond! 572791!
To mars and beyond! 572792!
To mars and beyond! 572793!
To mars and beyond! 572794!
To mars and beyond! 572795!
To mars and beyond! 572796!
To mars and beyond! 572797!
To mars and beyond! 572798!
To mars and beyond! 572799!
To mars and beyond! 572800!
To mars and beyond! 572801!
To mars and beyond! 572802!
To mars and beyond! 572803!
To mars and beyond! 572804!
To mars and beyond! 572805!
To mars and beyond! 572806!
.
.
.

If you changed the program… congrats!!!

Muck with the math…

Now, notice how this program is counting by one?  Try editing the program like we did earlier, but instead of changing what it says, make it count by two!

Again, edit the program, like this:

vim count.py

Move the cursor using the arrow keys to the line containing the

x += 1

And change the 1 to a 2:

x += 2

Again, save out the file esc:, w,

:wq

Again run it, and you should see it start to count by twos!  If so, Great!

Time to experiment!

Now try some other experiments.  Can you change the wording?  Can you make it count by other numbers?

Lets try a little input

Again, get to the directory containing all of the python code.  There is another program there called stinky.py:

ls
count.py papirus-btc papirus.service pong.py  stinky.py

Run that program:

python stinky.py

You’ll see it asks for a little input.  Go ahead and enter your name:

Enter your name: Matt
Hello Matt

This program introduces input.  It also reinforces the concept of variables.  Now edit the program and see how it accomplishes this:

Screen Shot 2017-05-21 at 6.41.04 AM

The first line creates and fills the variable called name the second uses the variable and creates a statement with it.

Try taking in two different values and creating an output with both.  Here you should try out the yy  command in VIM.  It yanks a line into one of its memory buffers, and makes it available for the p command for pasting.  Move your cursor to the first line.  Hit ESC-yy-p.  You should be left with:

Screen Shot 2017-05-21 at 6.44.41 AM

Now notice that the variable name is there twice.  The computer will now ask the questions and fill that SAME variable overwriting the first value with the second response.  So we need a NEW variable to take the new value.  Try name2.  Something to mention: Variable names cannot have spaces in them.  When I did this the kids attempted variable names like: last name this wont work.  They also should start with numbers like this: 2name

Try lastname and change the input string to ask for the second name:

Screen Shot 2017-05-21 at 6.48.30 AM

Notice in the above program how the lastname is now a part of the output?  The quoted space adds a little distance between the two names so it doesn’t come out slammed together.

Now… run this program and experiment!

Teacher’s note:

This worked *way* better than I expected.  The kids went nuts seeing the computer count to bazillions in no time.  They surprised me(cause I hadn’t tried it in python yet) with how high a number could put in the line: x+=1.  They put in numbers that scrolled around the line four or five times and python dutifully ran.

Additionally, I needed a little more material for two hours.  They blew through the counting exercise and probably would have played with that for the remainder of the day but adding the input program easily ate the rest of the time.

Onward…

Lessons on PI – Lesson 3: Getting around (ls, cd, rm, mkdir, touch, pwd, less…)

Advertisement

3 thoughts on “Lessons on PI – Lesson 2(cont.) A little programming…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.