Learn 3: Create Your First Class
This is the fifth challenge within the Robotics with GoPiGo and Python Curriculum.
Your task is to design a robot program to deliver a selection of mail to the residents of GoPiGo Drive, a small cul-de-sac containing 3 houses.
Learn how to how to use and calibrate the color sensor.
1. Variables: Global vs Local Scope
You may have noticed that we declared the relevant sensor instance variables as global within our test functions before using them. e.g.
def test_line_follower( ):
global line_follower
This allows the code within the function to interact safely with the globally scoped sensor instances that we created in the Sensor Setup Code cell.
Any variable that is defined outside of a function definition is said to be globally scoped. Variables that are defined within a function definition are said to be locally scoped (visible to that function only) unless we declare them as global by using the global keyword.
The interaction between local and global variables can become complicated so we're going to take an approach called defensive programming.
Defensive Programming: when you write code that is not really necessary but makes things more obvious to you as the programmer and to the computer too, in order to avoid bugs in the future. It's similar to looking both ways before crossing the street, even if you do not hear any car coming.
As for global and local scope, you can read this external in-depth tutorial if you want to know more about the interaction between the two scopes.