-2

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

martineau
  • 119,623
  • 25
  • 170
  • 301
Red Beast
  • 3
  • 1
  • 1
    Count the number of parentheses, they should match... – DavidG Apr 16 '18 at 21:18
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Apr 16 '18 at 21:18
  • And please format your code properly. Python indentation is important! – DavidG Apr 16 '18 at 21:19
  • Looks to me like you are missing a closing `)` on the statement `print ('Your command was: ' + command + '/n'`. These kinds of error messages can be misleading because the problem often has occurred elsewhere in the code—i.e. **not** actually the place that's causing the problem. – martineau Apr 16 '18 at 21:29
  • Thanks a lot,that fixed the problem,but when i fixed it i ran into this problem can you please help me? this is the error that python displays : unindent does not match any outer indentation level and it's location is next to unknown value error Thanks a lot! -Tudor @martineau – Red Beast Apr 16 '18 at 21:38
  • RedBeast: It's very difficult to tell since I can't see your revised code—but it sounds like an indentation problem. The `try` and the `except` have to be at the same level of indentation. Also, make sure you aren't mixing tab and space characters when indenting lines because they aren't equivalent in terms of the _amount_ of space they're considered to be adding. – martineau Apr 16 '18 at 21:44

1 Answers1

0

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

  except sr.UnknownValueError:
      assistant(myCommand())) # close parentheses, indent this line
Colin Ricardo
  • 16,488
  • 11
  • 47
  • 80