CyberPi: Python Quizzes
In this lesson, students will be introduced to the features of the CyberPi device. Students will explore some commonly used input and output devices of CyberPi, including the buttons, joystick, display screen and LEDs. Students will also investigate how to utilize these physical components to create a simple keypad along with using the keypad to answer quizzes on Python or other topics/subjects.

Lesson
Overview
Description
In this lesson, students will be introduced to the features of the CyberPi device. Students will explore some commonly used input and output devices of CyberPi, including the buttons, joystick, display screen and LEDs. Students will also investigate how to utilize these physical components to create a simple keypad along with using the keypad to answer quizzes on Python or other topics/subjects.
Objectives
Concept of APIs, specifically the CyberPi
How to import CyberPi into Python Button, joystick, screen, and LED controls of CyberPi using Python
Before the Lesson
Preparation
For the teacher:
Students should have a laptop or device with mBlock 5 installed from here: https://www.mblock.cc/en/download/ (desktop version or cloud-based version) and mBlock Python editor (mLink) installed. Click here to download the Python editor (mLink)
One CyberPi with USB-C cable
Pocket shield (optional)
Worksheet: Lesson 9 Python Quizzes
Example Program: Lesson 9 Python Quizzes
Refer to the text-based beginner lessons in this bundle for more ways to teach text-based programming without the use of the CyberPi.
This lesson may not able to complete in one class. If so, break the lesson into two days to allow students to get comfortable with the CyberPi.
Introduction
This lesson will introduce the features of the CyberPi device.
Students will explore some commonly used input and output devices of CyberPi, including the buttons, the joystick, the display screen, and the LEDs.
Students will also investigate how to utilize these physical components to create a simple keypad.
Students will use the keypad to answer quizzes on Python or other topics.
Review
Warm Up
1. Let's begin by familiarizing ourselves with the key points of this lesson and the CyperPi device:
Display and introduce the CyberPi device.

Ask students to observe their devices. Have them identify the physical components of CyberPi and fill in the blank on the worksheet.
2. Instruct students to connect CyberPi to the laptop or desktop.
Demonstrate how to use the USB Type-C cable to connect CyberPi to the laptop or desktop.
For more information on how to connect, see steps 3-6 of the Tour of mBlock5

3. Introduce the concept of API (application programming interface).
Say: How can we program CyberPi in the mBlock Python editor? If we want to display text on the screen, what functions do we need to use? Could we display text on the screen just with ‘print()’ (e.g. print(“Hello, CyberPi!”))?
Explain: To program CyberPi in the Python editor, we need to understand how to use the application programming interface of CyberPi. The application programming interface (API) of CyberPi is a computing interface which defines interactions between CyberPi and the Python editor. It allows us to write Python code in the editor to program CyberPi. As this API is not built in Python, we need to write the statement ‘import cyberpi’ as the first line: import cyberpi
4. Explain the use of the ‘cyberpi’ module as the prerequisite for programming CyberPi in Python.
Have students recall when they need to use the ‘import’ module. Ask them to explain why they need to use this syntax.
Give an example: When we want to model a probability problem in Python, we need to import the ‘random’ module to generate a set of numbers. When the ‘random’ module is added, we can use functions and expressions such as ‘random.randint()’ to create the sequence of integers.
Explain: We need to import the ‘cyberpi’ module; otherwise, we cannot program CyberPi in Python.
5. Have students fill out the ‘What I Know’ column of the K-W-L chart before the class.

Direct Instruction
Tour of mBlock 5
1. Open the mBlock software or mBlock 5 Web version.
2. Introduce students to the following key areas of the software interface:


Connect the CyberPi
3. Plug the CyberPi into the computer using the included cable. The CyberPi should boot up and the screen will display either the last program uploaded or the Home menu.
4. On the "Devices" tab in mBlock, click the "Add" button. Select CyberPi and click "Ok".
5. Click the "Connect" button. Then, select the USB port and click "Connect".

6. If connected successfully, the button will change to "Disconnect".

7. When you open the editor for the first time you will see these explanations mapping the interface. Encourage students to thoroughly read through it. Below are the screen captures if you ever need to reference them again.
Figure 1: To connect your CyperPi

Figure 2: Creating Projects and Project files (see How to Create, Open, Import, or Export Project Files)

Figure 3: Editing Area

Figure 4: Console Commands to run code and more

Figure 5: Terminal (Console) to display output from Editing Area

Figure 6: Libraries containing example program. Feel free to explore these at your leisure

Figure 7: Click "Tutorials" to view these helpful instructions anytime

Guided Practice
Hands On
Activity 1:
1. Have students read "Activity 1" in the example program
Say: The example program is a simple quiz application. Use the button ‘A’ or ‘B’ to answer the true or false question displayed on the screen.
You can provide some hints to point out the physical components CyberPi has and how these physical components are programmed.


2. Ask students to think about the questions below:
Which module is imported in this program?
Explain why we should import this module.
How to print text on the screen?
How to light up/off the LEDs?
How to set the LEDs’ colour? How to represent LEDs’ colours in Python?
How to enter the answer?
How to evaluate the answer?
3. Ask students to explain the program and draw a flowchart.

Activity 2:
1. Explain how to display text on CyberPi’s screen.
Say: We cannot write the code – for example, ‘print(“Hello, CyberPi!”)’ - in the Python editor to display the text on CyberPi’s screen. We need to call a function that allows us to program the display screen of CyberPi.
Emphasize the use of API: To program the screen or other physical components, we need to know the Application Programming Interface (API) of these components. An API enables data transmission between the Python editor and CyberPi. The API of a physical component specifies executable code on request. For instance, if we want to display ‘Hello, CyberPi!’ on CyberPi’s screen, we need to know the API of the screen.
2. Point out the API code samples of CyberPi’s screen to the students:
cyberpi.console.print()
cyberpi.console.println()
Demonstrate how to display the text ‘Hello, CyberPi!’ on the screen. Instruct students to run the code shown below:
import cyberpi
cyberpi.console.print(“Hello, CyberPi!”)
Explain the difference between the ‘cyberpi.console.print()’ and ‘cyberpi.console.println()’. Explain that the latter makes the text go to the next line automatically while printing out the new content.
3. Instruct students to run and compare the two examples:
Example 2(a)
import cyberpi
cyberpi.console.print(“Hello, CyberPi!”)
cyberpi.console.print(“I can program in Python.”)
Example 2(b)
import cyberpi
cyberpi.console.println(“Hello, CyberPi!”)
cyberpi.console.println(“I can program in Python.”)
4. Explain how to create a new line of output with this function:
cyberpi.console.println(“”)
Remind students that if they want to clear the screen, they can use:
cyberpi.display.clear()
Remind students that they should pay attention to the letter case while reading and writing code.
4. Instruct students to annotate the presented API code samples in the example program.

5. Explain how to program the LEDs.
Introduce the API code samples of the LEDs used in the example program:
cyberpi.led.on()
cyberpi.led.off()
Introduce other API code samples of the LEDs for reference.
cyberpi.led.on(“green”, id=“all”)
cyberpi.led.on(“red”, id=3)
cyberpi.led.show(“orange yellow cyan blue purple”)
cyberpi.led.play(name=“firefly”)
cyberpi.led.off(id=“3”)
Instruct students to annotate the presented API code samples in the example program.
6. Explain how to program the two buttons.
Say: By pressing Button A or Button B, we choose the answer true or false.
Introduce the API code samples of the buttons used in the example program:
cyberpi.controller.is_press(“up”)
cyberpi.controller.is_press(“down”)
cyberpi.controller.is_press(“right”)
cyberpi.controller.is_press(“left”)
cyberpi.controller.is_press(“middle”)
Independent Practice
Try It
Activity 3
Have students work individually and complete the following tasks:
Task 1: Modify the quiz and the answers in the example program. Below are some example questions:
In Python, 200.0 is an integer. (False)
‘True’ and ‘true’ are the same in Python. (False)
The word ‘break’ can be used to name a variable. (False)
To program CyberPi, you should import ‘cyberpi’. (True)
The ‘input()’ returns a string. (True)
Task 2: Modify the lighting effects.
Remind students that they can design the lighting effects or use the default effects.
The default lighting effects include:
Firefly: cyberpi.led.play(name=“firefly”)
Rainbow: cyberpi.led.play(name=“rainbow”)
Spoondrift: cyberpi.led.play(name=“spoondrift”)
Meteor Shower: cyberpi.led.play(name=“meteor_blue”); cyberpi.led.play(name=“meteor_green”)
Flash: cyberpi.led.play(name=“flash_orange”); cyberpi.led.play(name=“flash_red”)
Task 3: Use the joystick instead of the buttons to enter the answer.
Instruct students to use the joystick’s ‘up’ and ‘down’ input to decide the ‘True’ and ‘False’ options.

Differentiation: Encourage students to consider a program for multiple-choice questions that provides five options. Use the joystick as an input device to enter the possible input responses: ‘Option: A’, ‘Option: B’, ‘Option: C’, “Option: D”, and “Option: None of the Above”.
Below is a more advanced implementation:

Reflection
Have students fill out the K-W-L chart
Remind students to always comment their code, regardless of the simplicity

Extension
If students have not already done so, encourage them to complete the advanced implementation for Activity 3, Task 3.
Wrap Up
Wrap-Up & Quick Check
Summary
1. The concept of an API and how CyperPi was used to complete the activities.
2. CyberPi’s physical components.
Downloadable Material
Example Program
Worksheet
Educational Standards
Ontario - Grade 9-10 - Mathematics & Computer Science
Grade 9 Math (Coding)
Coming Soon!
Grade 10 Computer Science
B1: ICS20 Arithmetic Operations
B1.1 use correct terminology to describe programming concepts;
B1.2 describe the types of data that computers can process and store
B1.4 determine the expressions and instructions to use in a programming statement, taking into account the order of operations
B1.5 identify situations in which decision and looping structures are required;
B1.6 describe the function of Boolean operators
B2: Writing Programs
B2.1 use a visual problem-solving model to plan the content of a program;
B2.2 use variables, expressions, and assignment statements to store and manipulate numbers and text in a program
B2.3 write keyboard input and screen output statements that conform to program specifications;
B2.4 write a program that includes a decision structure for two or more choices
B2.5 write programs that use looping structures effectively
B3: Code Maintenance
B3.1 write clear and maintainable code using proper programming standards
B3.3 use a tracing technique to understand program flow and to identify and correct logic and run-time errors in a computer program;
B3.4 demonstrate the ability to validate a computer program using test cases.
