From the course: Building the Classic Snake Game with Python

Unlock the full course today

Join today to access over 24,600 courses taught by industry experts.

Global variables

Global variables

- Now is a good time to talk about global variables, as we just used one in the last program. This was the variable, my_turtle. We're now going to take a little detour to explore this concept. The snake game program we're going to write makes use of several global variables. These are variables which exist outside of any functions in a program, but which can be used inside particular functions if required. You may have heard that global variables are bad and should be avoided. However, this is an oversimplification. While there are many situations where it is a good idea to avoid them, the global keyword exists in Python for a reason, and using global variables for a program like the snake game we're writing makes sense as not doing so would make the program a lot more complex. Context is important. To understand how global variables work, let's take a look at an example. So, if I define a variable called name and I set it equal to Susan And I print the name. Just make sure…

Contents