3.2.4.5 Lab – Writing Python Scripts Using Blockly (Instructor Version)
Objectives
- Access the Official Course Materials from PL-App
- Create a new Visual Programming application
- Create a Python Script using Blockly
Background / Scenario
The syntax of programming can sometimes get in the way of the logic. Blockly provides a graphical way for programs to be developed without having to worry about the details of a particular language.
Required Resources
- PC with Internet Access
- Ethernet based connection to the Internet with DHCP and no traffic filtering
- Raspberry Pi that is configured and imaged for PL-App access
Task1: Learn Blockly
Step 1: Create a Visual (Blockly) Programming cell.
a. Enter the Code cell bellow and turn it from a default Python code cell to a Visual Programming cell by clicking on the Blockly icon in the toolbar.Chestnut Toolbar with Blockly
# turn this cell into a Visual Programming cell by clicking on the Blockly icon in the toolbar
Step 3: Create a program Using Blockly
1. The code cell now supports Visual Programming using Blockly. Add a new Print block and Text block to the canvas from the “Program->Text” menu.
2. Connect the Text block to the Print block. Add to the Text block the follwing text: This program will sort two words alphabetically.
3. Select the Program > Variables > set item to block.
4. Place the block below the Print on screen block.
5. From the “Program->Text” menu, select and drop to the canvas the prompt for text with message block and connect it to the existing set item block. Update the text part of the prompt for text with message block to What is the first word?
6. Change the set to variable name to Word1
.
7. Right click the red area on the set to block and select Duplicate . Reposition the new block directly below the previous set to block.
8. Using the arrow next to Word1
in the bottom set to block, select New Variable…
and type Word2
as the new variable name.
Note: Using the Rename variable… option will change the variable in both set to blocks to Word2, as depicted below. This is not what you want.
9. Select the Program > Logic > if-do block and insert it below the previous three blocks.
10. Select the Program > Logic > equal block and append it to the if-do block.
11. Select Program > Variables > Word1 and insert it into the first variable location in the equal block. Select Program > Variables > Word2 and insert it into the second variable location of the equal block.
12. Change the equal symbol to a less than symbol
13. Add two Print blocks on the canvas from the “Program->Text” menu into the if block.
14. Add the variables Word1 and Word2 to the Print blocks.
15. Select the else statement using the star located at the top of the if block, and drag it to the right onto the if icon.
16. Duplicate the Print on screen statements using the right mouse button, and place them in the else area in reverse order.
17. The competed project should resemble the program depicted below. If it does not, make changes accordingly.
18. Examine the Python source code using the generated code on the right side of the screen.
Step 4: Run the Application.
In the code cell, click on the Play button at the top left corner of the cell. The Python code will be executed on the Raspberry Pi with the input and output handled via this notebook page.
Reflection
What would the output be if the less than symbol was changed to the greater than symbol in the WordSorter program?