You need the global statement:
if G is None:
G = 1
In Python, variables that you assign to become local variables by default. You need to use global to declare them as global variables. On the other hand, variables that you refer to but do not assign to do not automatically become local variables. These variables refer to the closest variable in an enclosing scope.
Python 3.x introduces the non local statement which is analogous to global, but binds the variable to its nearest enclosing scope. For example:
x = 5
x = x * 2
This function returns 10 when called.