Bugs that don’t generate error messages
As we saw from the previous section, there are some errors, such as those caused by indentation errors, that may not always be caught by the debugger or throw the usual error messages. In this section, I want to concentrate on one of the most common errors, which can wreak havoc in our algorithms – errors caused by the incorrect use of local and global variables.
Global variables
A global variable is a variable that we declare outside of functions. Seems simple enough, right? It is. However, beware of overusing global variables, as they can cause a lot of problems with your code and make debugging a pretty hard problem to solve.
Let’s take a look at a global variable and its use inside a function:
ch13_global.py
counter = 1
def counterF():
print(f'Counter value is {counter}.')
counterF() As you can see, we created a variable called counter and then used the variable in a function called...