title: def in python not working when called
body: <pre><code>import pyttsx 
  import random
  import os  
  import serial
  import webbrowser
  
  while True:
      def portport():
          ser = serial.Serial('COM3',9600) 
          raw_data = ser.read(7)
          msg = str(raw_data[3:6])
          print msg
          ser.close()
  
      engine = pyttsx.init()
      portport()
      if(msg == 'mof'):
              engine.say('Are you sure?')   
              engine.runAndWait()          
              portport()
              if(msg == 'yes'):
                  engine.say('Chrome has now closed')
                  os.system("TASKKILL /F /IM chrome.exe")
                  engine.runAndWait()
  
              elif(msg == 'noo'):
                  engine = pyttsx.init()
                  engine.say('Are you kidding me?')
                  engine.runAndWait()
  
      if(msg == 'hi_'):
              greeting()
      if(msg == 'bye'):
              leaving()
      if(msg == 'who'):
              engine = pyttsx.init()
              engine.say('Hi there')
              engine.runAndWait()
  
      if(msg == 'lik'):
             engine = pyttsx.init()
             engine.say('Ofcourse I do!')
             engine.runAndWait()
  </code></pre>
  
  <p>My problem is that everything works if instead of creating a def (<code>portport()</code> ) and call it when I need it, I just write my code which is included in this def. I used (but didn't put in here because I don't want it to be a very long piece of code) other functions (for example <code>greeting()</code> ) and it worked just fine.</p>
  
  <p>Eclipse's console when using the code as above "says":</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\xxx\eclipse\workspace\Ai_Project\Ai\Ai_Brain.py", line 126, in &lt;module&gt;
      if(msg == 'mof'):
  NameError: name 'msg' is not defined
  </code></pre>
  
  <p>And every single <code>if(msg == 'something'):</code> turns red</p>
  
  <p>I have read a lot of posts here and everywhere but can't seem to be a python function problem</p>
  
  <p><strong>THE ANSWER:</strong>
  Thank you all for your valuable answers and a little bit more for mhawke's answer.
  The code worked after the chances which had to be made according to your suggestions. For future use the updated code is:</p>
  
  <pre><code>.....
  
      def portport():
  
              ser = serial.Serial('COM3',9600) 
              raw_data = ser.read(7)
              msg = str(raw_data[3:6])
              print msg
              ser.close()
              return msg
  
      while True:
  
              engine = pyttsx.init() 
  
              msg = portport()
  
               if(msg == 'mof'):
                          engine.say('Are you sure?')   
                          engine.runAndWait()          
                          msg = portport()
  
                          if(msg == 'yes'):
                              engine.say('Chrome has now closed')
                              os.system("TASKKILL /F /IM chrome.exe")
                              engine.runAndWait()
  
                          elif(msg == 'noo'):
                              engine = pyttsx.init()
                              engine.say('Are you kidding me?')
                              engine.runAndWait()
                                                                        ......
  </code></pre>