title: Python error: NameError: name is not defined
body: <pre><code>import numpy as np
  from scipy import optimize as opt
  import time
  
  def grad_d():
      weight = [0, 0]
      learnrate = 0.01
      tol = 1.e-5
      itmax = 1000
  
      for i in range(itmax):
          deltaweight = - learnrate * opt.rosen_der(weight)
          weight = weight + deltaweight
          if abs(deltaweight) &lt; learnrate:
              break
      
      return weight, i
      
  print('Weight: ', weight)
  print('Iterations: ', i)
  </code></pre>
  <p>After running the code, I receive error message:</p>
  <p>&quot;File &quot;c:/Users/Desfios 5/Desktop/Python/gradientdescent.py&quot;, line 19, in 
  print('Weight: ', weight)
  NameError: name 'weight' is not defined</p>
  <p>I am new to Python and I do not understand why it is saying that the variable &quot;weight&quot; is undefined when it clearly appears to be defined under grad_d().</p>