The syntax error in your code is occurring because you are trying to declare q as a global variable inside the function example() after already passing it as a parameter. In Python, you cannot use the global keyword to declare a variable as global within a function if it has already been defined as a parameter.
To fix this issue, you can either remove the parameter q from the function definition or choose a different name for the global variable. Here's an example:
q = 8
def example():
global q
q = q + 2
print(q)
example()
In this updated code, the q variable is declared as a global variable outside the function, and then inside the function, the global keyword is used to indicate that we want to modify the global variable q. The output will be 10, which is the result of adding 2 to the initial value of q.
Remember that using global variables should be done with caution, as it can make code harder to understand and maintain. It's generally recommended to pass variables as parameters or return values from functions instead of relying heavily on global variables.