Search code examples
pythonerror-handlingsyntax-errortry-catchexcept

SyntaxError when trying to handle errors with except


I'm trying to program a personal assistant for my ICT class. I'm a beginner in python and I tried following a tutorial but I get a SyntaxError when using except.

This is the script:

try:
    command = r.recognize_google(audio)
    print ('Your command was: ' + command + '/n'
    #loop back ca sa asculte pentru comenzi daca nu gaseI'ste recon speech
except sr.UnknownValueError:
    assistant(myCommand())

And here's the error:

File "/Users/dumitruvictor/Desktop/Assistant.py", line 31
    except (sr.UnknownValueError:
         ^

SyntaxError: invalid syntax

Operating System: Mac OS X


Solution

  • You need to close the parentheses and indent the line after the except:

      except sr.UnknownValueError:
          assistant(myCommand())) # close parentheses, indent this line