I have the following scripts:
the first is test1.py
x = 1
def test():
globx = x
print(globx)
globx += 1
return globx
x = test()
and the second is test2.py
import test1
z=3
while z != 0:
if __name__=='__main__':
test1.test()
z -= 1
else:
pass
I'm using these to learn and play about with calling functions from other scripts, what I want is the output:
1
2
3
but I get:
1
2
2
2
when I replace the
globx = x
print(globx)
globx += 1
return globx
x = test()
in test1.py with
global x
print(x)
x += 1
I do get the desired outcome but apparently you shouldn't use global variables so it's a habit I'd like to break.
I have 2 questions.
- Why do I get the wrong output and
- Why are there 4 outputs when I use the
globx = xversion oftest1.pyinstead of 3?