Online · Yokohama · Japan

mBlock Robot Building and Sensor Coding in Yokohama

mBlock robot building and sensor coding for Yokohama lets students write visual algorithms that control motor drives, line-following ideas and light indicators—on screen first, optional hardware later.

mBlock Yokohama

Video coming soon

Lesson notes and Q&A below are available now. The video will appear here when published.

1. Introduction: The Concept of Variable Scope

In Python programming, functions are reusable blocks of code created to execute specific instructions whenever called. When writing functions, programs inevitably declare and manipulate variables.

However, where a variable is created determines where it can be seen and used across the rest of the application. This boundary of visibility and accessibility is known as variable scope.

In Python, variable scope is divided into two primary categories:

  1. Local Variables: Variables defined inside a function.
  2. Global Variables: Variables defined at the main program level, outside of any function.

2. Local vs. Global Variables Explained

  • Global Variables: When you create a variable at the root level of your script, it belongs to the global scope. It is visible and accessible by default across functions in the program.
  • Local Variables: When you create a variable inside the indented body of a function, it is private to that function. Its existence begins when the function executes and ends when the function finishes. It cannot be referenced or accessed outside that function.

3. Why Global Variables Are Discouraged (Best Practices)

While Python technically allows functions to read global variables directly, relying on global variables across multiple functions is considered poor programming practice.

  • Tracking Difficulties: If a global variable is modified by several different functions, it becomes difficult to track down which function introduced a change or bug into that variable.

The Recommended Approach:

  1. Rely on local variables inside functions.
  2. Pass program-level data into functions cleanly as input parameters.
  3. If the main program needs a result produced inside a function, send it out using the return keyword and capture it into a global-scope variable.

4. Code Walkthrough: Precedence and Scope

Step 1: Defining a Function with a Local Variable

In this code, a function named show() defines a local variable x with the value 5 and prints it:

# Defining the function with a local variable x
def show():
    x = 5
    print(x)

Running this definition creates the function blueprint in memory without producing immediate screen output.

Step 2: Defining a Global Variable and Testing Function Precedence

Next, define a global variable with the same name x at the program level, set its value to 10, and execute show():

# Defining a global variable at program level
x = 10

# Calling the function
show()

Output:

5

How Python Evaluates This:

  • The program has two variables named x: a global x equal to 10 and a local x inside show() equal to 5.
  • When show() executes, Python gives precedence (priority) to the local variable over the global variable.
  • As a result, the function accesses its own local x and prints 5 rather than the outer value 10.

Step 3: Accessing Variables Outside the Function

If you execute a print(x) statement outside the function in the global scope:

# Printing x in the global program scope
print(x)

Output:

10

How Python Evaluates This:

  • Because this print(x) statement sits outside the function, the local variable inside show() (x = 5) is completely inaccessible.
  • Python retrieves the global variable x, printing 10.
  • If you need to access values calculated inside a function in the outer program, the function must return that value so it can be captured in a variable at the global level.

Lesson Q&A

A local variable is declared inside a function's body and is accessible only within that specific function. It cannot be viewed or accessed from the outside program. A global variable is declared at the main program level outside of any function and is accessible throughout the program and across functions.

When a function contains a local variable with the same name as a global variable (such as a local x = 5 and a global x = 10), the local variable takes precedence inside the function. When called, the function will access and print its local value (5). Outside the function, however, the local variable is inaccessible, and referencing that variable name accesses the global value (10).

Relying on global variables makes programs difficult to maintain because when multiple functions interact with the same global data, it becomes hard to track which function modified the variable when bugs occur. Good programming practice recommends using local variables, passing needed data into functions as input parameters, and using the return statement to send computed results back to the global scope to be stored in variables.

What children can learn

WhatsApp +92 322 421 7059

AI and Robotics Club | The 100 Mission AI & Robotics Hero Journey

Recommended self-paced course for parents & educators

mBlock Coding Fast Track for Beginners on Udemy.

Disclosure: This page contains affiliate links. We may earn from qualifying purchases at no extra cost to you. Links are intended for parents and educators, not as marketing directed at young children.

Visual Algorithms Meet Motion: mBlock Practice for Yokohama Learners

Yokohama families gain from visual robotics that stays optional on hardware. Enquire WhatsApp +92 322 4217059. No invented Yokohama studio.

Frequently asked questions

What makes mBlock effective for robot building and sensor coding in Yokohama?

mBlock combines Scratch-style visual coding with real-time hardware execution. Students in Yokohama write block algorithms that control physical mBot motor drives, line-following sensors, and RGB light indicators in real time.

Can Yokohama students participate in mBlock classes without buying physical hardware first?

Yes! We utilize interactive virtual robotics simulators during online live sessions, enabling students to program and test robot movements on screen prior to purchasing physical hardware.

How can I request a free mBlock demo class for my child in Yokohama?

Click the WhatsApp button on this page to connect with our mentor team and secure a free live trial session this week.

Explore related learning

ShinjukuArduino · Shinjuku MinatoRobotics · Minato OsakaBootcamp · Osaka