kids · lessons · programming · python

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

Screen Shot 2017-05-21 at 7.08.22 AM

The conditional… or “IF” statement

Programs that count really fast or create MADLIBs are a ton of fun, but part of encoding knowledge is the ability to make decisions in code.  To do that we need something that will allow the code to take two different paths… the “if” statement.

The if statement checks a value for truth.  If it finds it, then it will execute the commands if owns.  If it doesn’t find truth, its code is not allowed to be run.

Let’s try it…

There is another program available in the kidpi git repository that will let us play with that concept:

cd /kidpi/python
ls  papirus-btc papirus.service

Run that program:


Screen Shot 2017-05-21 at 7.18.38 AM

I wonder what would have happened if I answered the OTHER way!  Lets try it:

Screen Shot 2017-05-21 at 7.21.49 AM

Hey!  So now the program is able to do two different things based on the user’s input!

So what’s happening there.  Lets take a look.

Edit the code…

You can see it builds on the program “” we played with earlier… but now there is that interesting question its asking… lets take a look:


Screen Shot 2017-05-21 at 7.08.22 AM

See the if statement?  That’s the new piece of magic we are using.  It takes the variable we created called isStinky, pulls out its value, and checks it against the right hand side.  In this case y.

Comparison Operators… ==, >, <, >=, <=, !=

There are some other hidden gems in that if statement.  The == in that line is funky too.  That tells the ‘if’ statement to check if things are equal before saying that is true.

(Why not use “=”? Because that is the bit of code reserved for “assignment” or “giving a variable a value”, so the folks who built the language couldn’t use it for truth testing.)

Comparison Operators… or Relational Operators are special things used in if statements to give you the ability to ask:

  • Is it equal?
  • Is it greater than?
  • Is it less than?
  • Is it less than OR equal?

From Google:

Screen Shot 2017-05-21 at 7.30.56 AM

A little copy and paste…

So now lets add another question to the code.  How about “Do you have soap?”

To make this easy we’re going to learn how to copy and paste in VIM.

Move your cursor to the line:

Screen Shot 2017-05-21 at 7.37.10 AM

Now type ESC SHIFT-V (where the SHIFT part here is the shift key), which will start a “highlight block”, then use your arrow keys to move the cursor all the way down below the print statement:

Screen Shot 2017-05-21 at 7.39.07 AM

Now hit y and the code will be copied into a memory buffer inside VIM’s memory banks.

Move your cursor below the last print statement like this:

Screen Shot 2017-05-21 at 7.41.09 AM

And hit p for “paste”.  Which will paste or place the contents from that memory bank at the place of the cursor, like this:

Screen Shot 2017-05-21 at 7.42.14 AM

Now lets make some changes… using the x key delete isStinky, and replace it with hasSoap by hitting the i key and typing in the the new name.

Then do the same for the question and the if and the print statements, until you have something like this:

Screen Shot 2017-05-21 at 7.47.35 AM And the whole program looks like this:

Screen Shot 2017-05-21 at 7.48.21 AM

Now lets run it and see what happens!


Screen Shot 2017-05-21 at 7.49.04 AM

Huzzah!  We now have a program that has asked two questions!  Now you can do a full questionnaire!  Simple programs like this can help people or robots make decisions!

Try adding a couple more questions!


Lessons on PI – Lesson 4: The Sense Hat (and a little VIM)


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

Leave a Reply

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

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

Twitter picture

You are commenting using your Twitter 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.