In the previous lesson, the students learned how to output a message to the shell and the SenseHAT LED Matrix. This is an example of controlling the output of a program. Later the students will also learn how to output to another machine. In this lesson, we will build on this knowledge and continue to learn the structure of a program: Input - Process - Output, beginning by studying what inputs are and how to use inputs.
input()
functioninput, output, programming flow
Raspberry Pi 3 with SenseHAT
input()
function to customize their input.It should be obvious that most, if not all, programs are devoted to gathering data from outside, processing it, and providing results back to the outside world. That is, input and output are key.
Real World Haskell, Bryan O’Sullivan, Don Steward, & John Goerzen.
In the previous lesson, we wrote a program like this::
from sense_hat import SenseHat
sense = SenseHat()
sense.show_message("Hello Python", scroll_speed = 0.05,
text_colour = [255, 0, 0], back_colour = [0, 0, 255])
Begin the class by reviewing these questions. The students may discuss these questions in groups first and then share these questions with the rest of the class.
This image below could help you answer the above questions:
The above image demonstrates what a program consists of. We can think of programs as the mysterious machine shown in the image. It takes some input, do some processing, and produces some output. In the case of the program we have written, we know that we output the message to the shell/SenseHAT. More formally, a program looks like this:
Then, what is our input in this program? Ask the students to discuss in groups and guess.
The inputs are basically the parameter values that we pass to the
sense.show_message()
and print()
functions. If this is not clear
enough, we can actually restructure our program this way so that this
structure is clearer.
# Initiation:
from sense_hat import SenseHat
sense = SenseHat()
# Input:
msg = "Hello Python!"
speed = 0.05
red = [255, 0, 0]
blue = [0, 0, 255]
# Process
# Output
sense.show_message(msg, scroll_speed = speed,
text_colour = red, back_colour = blue)
The pound/hashtag sign #
starts a comment. You can write your notes in
the code this way and Python will ignore it. Other languages may use
double slash //
to do so. Here we use comments to divide our code into
initialization-input-process-output sections. Comments will be greyed
out in Thonny Python, which also means that these comments will be
ignored.
We only need to initialize the program once. We usually import libraries at the beginning of our code.
Notice that the # Process
block. Are there no processing step in our
program?
What did we just do in this program?
Well we just defined a few variables at the beginning of program and
then passed them to the sense.show_message()
function as parameters.
Take a look at the Variable panel in your Thonny Python:
TODO: add a Thonny Python screenshot.
When Python reads Line 14, it will automatically look up all these variables and substitute them with their values. So the code will eventually become something we are familiar with:
sense.show_message("Hello Python", scroll_speed = 0.05,
text_colour = [255, 0, 0], back_colour = [0, 0, 255])
It is better to demonstrate the process of substituting values with variables to the students.
We don’t need to write all programs this way but since we are learning to code, it is a good idea for us to stick to this structure for a while so we understand what is going on.
In the previous class we wrote code to have Python output 3 messages to SenseHAT/shell. Rewrite the program using the input-process-output structure provided above and still have Python output three messages.
You might want to have different variables as inputs, such as msg1
,
msg2
, msg3
, etc.
input()
function (15 minutes) So far we have only pre-defined messages for SenseHAT to print. Can we get this input dynamically from the user using our program? In other words, can we prompt the user to input a message that we can then output to SenseHAT/shell?
The input()
function can help us do that:
>>> input("What is your name?\n")
What is your name?
Captain America
'Captain America'
What do you think the function does?
The input()
function takes a string. It will display this string in
the shell as a prompt, and the user can type in something there. Then
the input
function will take whatever the user typed in the shell and
return it. What is the type of the returned value?
The \n
is a newline
character, so that you can answer the question
on a new line.
We can also save the returned value of this function to a variable so we can use it later:
>>> name = input("What is your name?\n")
What is your name?
Captain America
>>> name
'Captain America'
>>> "my name is " + name
'my name is Captain America'
Of course, we can output result to SenseHAT:
# Initiation:
from sense_hat import SenseHat
sense = SenseHat()
# Input:
name = input("What is your name?\n")
speed = 0.05
red = [255, 0, 0]
blue = [0, 0, 255]
# Process
msg = "Hello Python!" + name
# Output
sense.show_message(msg, scroll_speed = speed,
text_colour = red, back_colour = blue)
Challenge:
input()
function?