title: Python speech recognition error - Invalid number of channels
body: <p>I'm running a speech recognition code on python as part of a project. I'm facing a really odd kind of a problem 
  When I put the speech recognition code inside a function like:</p>
  
  <pre><code>def loop():
      r=sr.Recognizer()
      with sr.Microphone(device_index=2) as source:
              print("say something")
              audio = r.listen(source)
              try:
                      print("you said "+r.recognize_google(audio))
              except sr.UnknownValueError:
                      print("Could not understand")
              except sr.RequestError as e:
                      print("errpr: {0}".format(e))
  </code></pre>
  
  <p>It gives me the following error:</p>
  
  <blockquote>
    <p>with sr.Microphone(device_index=2) as source:   File "/usr/local/lib/python3.5/dist-packages/speech_recognition/<strong>init</strong>.py",
    line 141, in <strong>enter</strong>
        input=True,  # stream is an input stream   File "/usr/local/lib/python3.5/dist-packages/pyaudio.py", line 750, in open
        stream = Stream(self, *args, **kwargs)   File "/usr/local/lib/python3.5/dist-packages/pyaudio.py", line 441, in
    <strong>init</strong>
        self._stream = pa.open(**arguments) OSError: [Errno -9998] Invalid number of channels</p>
  </blockquote>
  
  <p>But if I run the same lines of code outside the function like not inside the <code>def loop():</code> it runs properly</p>
  
  <p>What should I do?
  My python version is 3.5.4</p>