title: Local variable referenced before assignment in Python 2.7
body: <p>I have very little experience with Python and I am just trying to make a function that gets the Greatest Common Divisor given an integer (number of elements in the list) and a list that comprises positive integers</p>
  
  <pre><code>def generalGCD(num, arr):
      if (num==2):
          gcd(arr[0],arr[1])
      else:
          gcd(arr[0],generalGCD(num-1, arr[1::]))
      def gcd(a, b) :
          if (b == 0):
              return a
          else:
              return gcd(b, a % b)
  </code></pre>
  
  <p>I am getting the following error:
  <code>UnboundLocalError: local variable 'gcd' referenced before assignment</code></p>
  
  <p>Can someone please tell why I am getting this error?
  Thanks</p>
  
  <p><strong>EDIT:
  My code is wrong, this is the correct way of doing it:</strong></p>
  
  <pre><code>def generalGCD(arr): 
    def gcd(a, b):
      if (b == 0):
        return a
      else:
        return gcd(b, a % b)
  
    if (len(arr)&gt;2):
      return generalGCD([gcd(arr[0],arr[1])] + arr[2::])
    else: return gcd(arr[0], arr[1])
  </code></pre>