Hi,
I am having trouble understanding the use of 'global' variables I want to use a global variable with the same name as a parameter of a function. But I don't know how to use them at the same time. Here is a snippet of example code:
When I executed this script, Python said "SyntaxErro r: name 'a' is local and global". I don't think there is anything wrong with Python, but just want to find a way to use local and global variable in one function. In CPP, we can use :: prefix to indicate the variable is global. But I have no idea that how to implement it in Python. Could anybody tell me the solution?
Thanks!
I am having trouble understanding the use of 'global' variables I want to use a global variable with the same name as a parameter of a function. But I don't know how to use them at the same time. Here is a snippet of example code:
Code:
def foo (a):
global p
global a
p = a + 3 #here "a" will be reference to the global one
a = 1
p = 0
foo (a)
print a, p
Thanks!
Comment