Setting Conditions for Yoga
Investigate using while loops
Questions to investigate
• How can a loop be used only when certain conditions are met?
Prepare
• Ensure SPIKE Prime hubs are charged, especially if connecting through Bluetooth.
• Ensure students have the Coach model built, which was used in the Dance Loop with Coach lesson.
Engage
(Group Discussion, 10 minutes)
Engage students in thinking about how conditional statements work.
Play a game of Teacher Says with students.
Rules of the game:
• Students have to follow the actions given by the teacher, but only when you say Teacher Says first.
• For example, when you say “Teacher Says jump”, students would jump.
• If you just say “jump”, then students would not jump because you did not say Teacher Says first.
Play a few rounds of Teacher Says with students.
Discuss with students the conditions set that indicate when to do the action. In the game, the condition for doing the action was Teacher Says. Students were only to do the act while the condition was true. For example, to add a bit of complexity using conditionals, you may try Teacher Says if you have shoes on, jump. Teacher Says if you are wearing a t-shirt, raise your arms.
Brainstorm other examples of when you do something based on a condition that is set (examples might be the weather deciding on your clothing, parents saying you can only have dessert after dinner, etc.). Discuss how you only do that action while the condition is true. For example, you generally only use an umbrella when it is raining. When it stops raining, usually you put the umbrella away. You stop the action when the condition is no longer true.
Explore
(Small Groups, 45 minutes)
Students will explore programming the Coach model to move using a while loop.
Direct students to open a new project in the Python programming canvas. Ask students to erase any code that is already in the programming area. Students should connect their hub.
Following Directions
Discuss with students that the Coach wants to ensure we follow directions to only move when he/she tells us. The condition Coach has set for our yoga workout is any number less than 5. Explain to students that the model should only move when that condition is true.
Provide students with the sample code to move the motor and create a yoga pose. Ask students to type this program into the programming canvas. Ask students to run the program.
from hub import port
import runloop
import motor
async def main():
count = 2
#run motors when count is less than 5
while count < 5:
# Run at 300 velocity for 1/4 second
await motor.run_for_time(port.B, 250, 300)
await motor.run_for_time(port.F, 250, 300)
await motor.run_for_time(port.B, 250, -300)
await motor.run_for_time(port.F, 250, -300)
runloop.run(main())
Allow students time to run and explore the program. In the sample program, we included the count as 2 just as an example. The purpose of the count variable is to assign a number therefore any number can be included. Prompt students to put new numbers or values in for the count variable (count=10, count=2, etc.). Point out to the students that count is a variable for which they can set any value. We are naming this variable count here because we want to count our exercise. However, students could name the variable anything if they use the same name to reference it later.
Explain
(Whole Group, 15 minutes)
Discuss with students how the program worked.
Ask students questions like:
• What was new in this program?
• What happens when you change the value assigned to the count variable? Higher than 5? Lower than 5?
• What is the “while” doing in the program?
• When does the program stop running?
• How is this an example of a loop?
Explain to students that the condition set in this program is for the motor to move only when the count variable is assigned a number less than 5. If the count is changed to 5 or higher than the model is not moving because we did not tell it to do anything else. The “while” is creating the conditional statement in the program to tell it only to work if the condition set is true.
To answer the last question, explain to students that while this is a good example of a conditional statement, it is not actually a loop right now. Nothing in the program is telling the action to repeat itself right now.
Elaborate
(Small Groups, 15 minutes)
Challenge students to turn this program into a while loop.
Discuss ideas with students of how to change this into a while loop.
Provide students with this sample code. Discuss the additional step added and how this will create a loop.
Ask students to run the program.
from hub import port
import runloop
import motor
async def main():
count = 2
#run motors when count is less than 5
while count < 5:
# Run at 250 velocity for 1 second
await motor.run_for_time(port.B, 250, 300)
await motor.run_for_time(port.F, 250, 300)
await motor.run_for_time(port.B, 250, -300)
await motor.run_for_time(port.F, 250, -300)
count += 1
runloop.run(main())
Allow students time to explore the program. Students should try different values for the count variable to see how the program works. Consider re-visiting the same questions from the explain section for discussion.
Challenge students to change the program to set different values for the while count, which can also include a greater than and equal to.
Evaluate
(Group Exercise, 15 minutes)
Teacher Observation:
Discuss the program with students.
Ask students questions like:
• What happened when you added the count+1 at the end of the program?
• How do while loops work?
• When is it helpful to use a while loop in a program?
Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about using while loops?
• What characteristics of a good teammate did I display today?
• Ask students to rate themselves on a scale of 1-3, on their time management today.
• Ask students to rate themselves on a scale of 1-3, on their materials (parts) management today.
Teacher Support
Students will:
• Investigate while statements.
• Program a model using while loops.
• SPIKE Prime sets ready for student use
• Devices with the SPIKE App installed
• Student journals
CSTA
2-CS-02 Design projects that combine hardware and software components to collect and exchange data.
2-AP-10 Use flowcharts and/or pseudocode to address complex problems as algorithms
2-AP-13 Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
2-AP-16 Incorporate existing code, media, and libraries into original programs, and give attribution.
2-AP-17 Systematically test and refine programs using a range of test cases.
2-AP-19 Document programs in order to make them easier to follow, test, and debug.