title: How to write a python script that can get and execute python commands?
body: <p>I'm pretty new to Python. I'm trying to modify a script so it will run in an infinite loop, will get Python code lines from the console and will execute the Python code lines.</p>
  
  <p>I'm talking about something that can do the following example:</p>
  
  <pre><code>Shell&gt; myconsole.py  
  &gt; PredefindFunction ("Hello")  
  This is the result of the PredefinedFunction: Hello!!!  
  &gt; a=1  
  &gt; if a==1:  
  &gt;     print "a=1"  
  a=1  
  &gt; quit  
  Shell&gt;  
  </code></pre>
  
  <p>I've tried using the exec() function. It works well with running the functions I've defined in my script, but it can't really execute all code for some reason. I don't get its logic. I get:</p>
  
  <pre><code>Shell&gt; myconsole.py  
  &gt; PredefindFunction ("Hello")  
  This is the result of the PredefinedFunction: Hello!!!  
  &gt; a=1  
  &gt; print a 
  ...
  NameError: name 'a' is not defined  
  Shell&gt;  
  </code></pre>
  
  <p>Can anyone help please?</p>
  
  <p>Thanks,<br>
  Gur</p>
  
  <hr>
  
  <p>Hi Kyle,   </p>
  
  <p>Here is the code:  </p>
  
  <pre><code>class cParseTermCmd:  
      def __init__(self, line = ""):  
          self.TermPrompt = "t&gt;"  
          self.oTermPrompt = re.compile("t&gt;", re.IGNORECASE)  
          self.TermCmdLine = ""  
          self.line = line  
  
      # Check if the TermPrompt (t&gt;) exist in line
      def mIsTermCmd (self):
          return self.oTermPrompt.match(self.line)
  
      # Remove the term prompt from the terminal command line  
      def mParseTermCmd (self):  
          self.TermCmdLine = re.sub(r'%s'%self.TermPrompt, '', self.line, flags=re.IGNORECASE)  
          exec (self.TermCmdLine)  
  
  
  And I call it in an infinite while loop from:  
  def GetCmd (self):  
              line = raw_input('&gt;')  
              self.TermCmdLine = cParseTermCmd(line)  
              if self.TermCmdLine.mIsTermCmd():  
                  # Execute terminal command  
                  self.TermCmdLine.mParseTermCmd()  
              else:  
                  return line  
  </code></pre>