3.2.4.5 Lab – Writing Python Scripts Using Blockly (Answers)

3.2.4.5 Lab – Writing Python Scripts Using Blockly (Instructor Version)

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 20

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

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 21

# 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.

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 22

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.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 23

3. Select the Program > Variables > set item to block.

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 24

4. Place the block below the Print on screen block.

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 25

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?

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 26

6. Change the set to variable name to Word1.

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 27

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.

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 28

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.

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 29

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.

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 30

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.

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 31

12. Change the equal symbol to a less than symbol

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 32

13. Add two Print blocks on the canvas from the “Program->Text” menu into the if block.

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 33

14. Add the variables Word1 and Word2 to the Print blocks.

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 34

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.

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 35

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.

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 36

18. Examine the Python source code using the generated code on the right side of the screen.

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 37

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.

3.2.4.5 Lab - Writing Python Scripts Using Blockly (Answers) 38

Reflection

What would the output be if the less than symbol was changed to the greater than symbol in the WordSorter program?

 

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x