title: Python Global Variables for Recursive Function
body: <p>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:</p>
  
  <pre><code>def main():
  
      global n
      n = 1
  
      def check():
  
          a = 10
          if n &gt; a: print(n)
          else: n += 1
  
      check()
  
  main()
  </code></pre>
  
  <p>This code fails because it says that <code>n</code> has not been defined yet. However, I can't define n inside of the <code>check()</code> function since that would just reset n to one upon every iteration! Is there some workaround for this problem?</p>