text: import random
  import time
  
  import SpeechRecognition as sr
  
  
  def recognize_speech_from_mic(recognizer, microphone):
  
      # check that recognizer and microphone arguments are appropriate
      if not isinstance(recognizer, sr.Recognizer):
          raise TypeError("'recognizer' must be 'Recognizer' instance")
  
      if not isinstance(microphone, sr.Microphone):
          raise TypeError("`microphone` must be `Microphone` instance")
  
  
      # adjust the recognizer sensitivity to ambient noise and record audio
      # from the microphone
      with microphone as source:
          recognizer.adjust_for_ambient_noise(source)
          audio = recognizer.listen(source)
  
      # set up the response object
      response = {
          "success": True,
          "error": None,
          "transcription": None
      }
  
      # try recognizing the speech in the recording
      # if a RequestError or UnknownValueError exception is caught,
      #     update the response object accordingly
      try:
          response["transcription"] = recognizer.recognize_google(audio)
      except sr.RequestError:
          response["success"] = False
          response["error"] = "API unavailable"
      except sr.UnknownValueError:
          response["error"] = "Unable to recognize speech"
  
      return response
  
  
  if__name__== "__main__"
  
      WORDS = ["apple", "banana", "grape", "orange", "mango", "lemon"]
      NUM_GUESSES = 3
      PROMPT_LIMIT = 5
  
  
      # create recognizer and mic instances
      recognizer = sr.Recognizer()
      microphone = sr.Microphone()
  
      # get a random word from the list
      word = random.choice(WORDS)
  
      # format the instructions string
      instructions = (
          "I'm thinking of one of these words:\n"
          "{words}\n"
          "You have {n} tries to guess which one.\n"
      ).format(words=', '.join(WORDS), n=NUM_GUESSES)
  
      # show instructions and wait 3 seconds before starting the game
      print(instructions)
      time.sleep(3)
  
      for i in range(NUM_GUESSES):
          # get the guess from the user
          # if a transcription is returned, break out of the loop and
          #     continue
          # if no transcription returned and API request failed, break
          #     loop and continue
          # if API request succeeded but no transcription was returned,
          #     re-prompt the user to say their guess again. Do this up
          #     to PROMPT_LIMIT times
          for j in range(PROMPT_LIMIT):
              print('Guess {}. Speak!'.format(i+1))
              guess = recognize_speech_from_mic(recognizer, microphone)
              if guess["transcription"]:
                  break
              if not guess["success"]:
                  break
              print("I didn't catch that. What did you say?\n")
  
          # if there was an error, stop the game
          if guess["error"]:
              print("ERROR: {}".format(guess["error"]))
              break
  
          # show the user the transcription
          print("You said: {}".format(guess["transcription"]))
  
          # determine if guess is correct and if any attempts remain
          guess_is_correct = guess["transcription"].lower() == word.lower()
          user_has_more_attempts = i < NUM_GUESSES - 1
  
          # determine if the user has won the game
          # if not, repeat the loop if user has more attempts
          # if no attempts left, the user loses the game
          if guess_is_correct:
              print("Correct! You win!".format(word))
              break
          elif user_has_more_attempts:
              print("Incorrect. Try again.\n")
          else:
              print("Sorry, you lose!\nI was thinking of '{}'.".format(word))
              break
  