title: Python. Name not defined error inside function
body: <p>I'm trying to do some exercises from &quot;Python crash course&quot; by Eric Matthews. I'm pretty sure I'm following the code in the book precisely, but I'm still getting &quot;Name not defined error&quot; when I try to run the program:</p>
  <pre><code>import sys
  import pygame
  from settings import Settings
  from ship import Ship
  
  class AlienInvasion:
      &quot;&quot;&quot;Class for managing game resources&quot;&quot;&quot;
      def __init__(self):
          &quot;&quot;&quot;initializing the game and creating resources&quot;&quot;&quot;
          pygame.init()
          self.settings = Settings()
          self.scr = pygame.display.set_mode((1200, 800))
          pygame.display.set_caption(&quot;Alien Invasion&quot;)
  
          self.ship = Ship(scr)
  
          .....
  
  if __name__ == '__main__':
      # Creating the game object and run the game
      ai = AlienInvasion()
      ai.run_game()
  
  </code></pre>
  <p>The error I get is:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;alien_invasion.py&quot;, line 43, in &lt;module&gt;
      ai = AlienInvasion()
    File &quot;alien_invasion.py&quot;, line 17, in __init__
      self.ship = Ship(scr)
  NameError: name 'scr' is not defined
  </code></pre>
  <p>If I understand correctly, program is trying to create an object of class &quot;Ship&quot; which is located in a separate module, but fails because it can not read the variable &quot;scr&quot;, even though it is defined two lines above, inside of the same method.</p>