What is programming?
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:
Ok, lets see what programs are there:
Try the program you saw there before:
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 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:
If 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 x 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:
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:
Running your program
Which brings you back to the prompt. You’ve just programmed! Lets try out your new program:
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:
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, q
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:
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:
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:
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:
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!
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.