My Code:
def A():
a = 'A'
print a
return
def B():
print a + ' in B'
return
When B() is entered into the interpeter I get
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "<module1>", line 9, in B
NameError: global name 'a' is not defined
How should I go about defining a? I want the end result to be ‘A in B’, when B() is entered into the interpreter
edit:
I’d like to keep the definition of a within A() if possible.
i’m pretty new to Python and you might want to take thes following with a grain of salt, but did you consider to have your variable a and the functions A() and B() as members of a class?
When i save the code above in a file and run it, it seems to work as expected.