title: Run String of Python Script inside a Function, Error in Return
body: <p>In <strong>Python</strong> this works:</p>
  <pre><code>exec('x = 1; y = 2')
  </code></pre>
  <p>so you have both x and y created successfully:</p>
  <pre><code>&gt;&gt;&gt; x,y
  (1,2)
  </code></pre>
  <p><strong>But this does not work:</strong></p>
  <pre><code>def DoIt():
      exec('x = 1; y = 2')
      return x,y
  
  &gt;&gt;&gt; DoIt()
  ...
  NameError: name 'x' is not defined
  </code></pre>
  <p><strong>Why? Any solution?</strong></p>
  <p>================================================</p>
  <p><strong>Edit:</strong>
  Very Strange and Unexpected:
  The above works on Python 2.7</p>
  <p>Check out on <a href="https://repl.it/languages/python" rel="nofollow noreferrer">https://repl.it/languages/python</a></p>
  <p><strong>Anyway, I am using Python 3.5 64Bit and it does not work at all! The Python 2.7 is not a solution for me.</strong></p>
  <p>This post requires <strong>in depth knowledge of an experienced Pythonist</strong>.</p>
  <blockquote>
  <p>Dear New Comers, first, very welcome to Stackoverflow! and WoW you started your journey with downvoting without even paying attention to what the question is all about! You can do better, be patient. Leave comments, it is OK and recommended.</p>
  <p>And BTW, what matters to me after all is <strong>an answer to my question.</strong></p>
  </blockquote>
  <p><strong>[Note]</strong></p>
  <p>This is not a duplicate of another question. I had to do no business with Python 2.7 to see a difference. Such bizarre behavior was discovered here following comments and previous (now deleted) answers.</p>
  <p>So when something like this happens, you do not go to check you code if running differently in previous versions of Python. You just do not. So recommendation on this regards is irrelevant.</p>