In my math class, we have been told to write a program in python. In one of the parts, I want to test the convergence of a series. While writing the program, I realized I was fundamentally misunderstanding something about how python handles global variables. Take this code:
def main():
global n
n = 1
def check():
a = 10
if n > a: print(n)
else: n += 1
check()
main()
This code fails because it says that n has not been defined yet. However, I can't define n inside of the check() function since that would just reset n to one upon every iteration! Is there some workaround for this problem?
nis not global inside ofcheck()- you'd need to put theglobal nthere, the one inmain()isn't actually accomplishing anything since nothing outside ofmainis accessing it.checkat all, let alone insidemain.