Now they've learnt how to setup the Pi, write some simple programs, flash an LED and detect a button - they're ready to start making some cool stuff!
The last 2 lessons focus on building a reaction timer and then running a class competition to see who has the fastest reactions.
Students have mostly copied code and made minor modifications. This time they'll work together to plan a program and to turn it into Python using pseudo code.
breakkeyword to break out of a loop.
Ask what hardware is needed? They react to the LED and push a button. Ask what software is needed? The LED has to light after a random time so they can't predict it.
Discuss with the class how the software will work.
Tell the students to get into groups of 4 and use the program cards to lay out a program sequence (from top to bottom). They'll need to use the blank ones to add their own variable names or extra codes.
After a few minutes ask 2 members of each team to walk around and get ideas from the other teams. They can ask questions, and the remaining members answer them.
My variable names are written in blue.
If you're feeling shaky with Python, then you can ask the whole class to agree to follow one team's program structure. This will make it easier to debug code later. Then take a photo and show it on the projector or ask someone to copy it onto the white board. Make sure you save it for the next lesson!
Ask students to get back in pairs, open a new program with Idle and save it as
Ask students to copy the code structure into their program, starting each line with a
# so it becomes a comment. The lines will change colour to remind them it's a comment.
It should end up something like this:
#pick a random number and store in sleep_time #sleep for sleep_time seconds #turn on the led #store the current time in start_time #in a loop: #if the button is pressed: #break out of the loop #work out how long they took by subtracting the start_time now from the time now #print out how long they took
We've introduced 2 commands with the program cards that they haven't covered yet in Python. These are also included in the handouts.
Here we import a new library called
random and then use
randint() to produce a random number between 1 and 5:
1 2 3
Here we use
break to end the
1 2 3 4 5
Students work in pairs to convert the comments into Python code. They can reference any of the programs they've saved so far. They should write the Python below the comment, and leave the comment in for reference.
Ask them to start at the top and to test each line of code at a time by saving and running the code.
If students get stuck, remind them to look at their previous programs:
stopwatchfor getting the time, delaying, printing and storing variables,
flashfor turning on and off LEDs,
buttonfor detecting a button press.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
Pack away the Pi.
Continue with the homework project from the last week. Students convert the software description they came up with to comments, and then start working on converting the comments into Python code.