My code:
#!/usr/bin/env python
def Runaaall(aaa):
Objects9(1.0, 2.0)
def Objects9(aaa1, aaa2):
If aaa2 != 0: print aaa1 / aaa2
The error I receive:
$ python test2.py
File "test2.py", line 7
If aaa2 != 0: print aaa1 / aaa2
^
SyntaxError: invalid syntax
I'm at a loss to why this error is happening.
-
2What python tutorial are you using to learn the language?S.Lott– S.Lott2009年10月15日 20:32:41 +00:00Commented Oct 15, 2009 at 20:32
-
1@S.Lott: :) I guess he hasn't followed your advice here stackoverflow.com/questions/1573548/…SilentGhost– SilentGhost2009年10月15日 20:35:59 +00:00Commented Oct 15, 2009 at 20:35
-
@SilentGhost: Good point. I rarely check to see who asks the question. This appears to be two questions with the same bad behavior: type random stuff and hope it works.S.Lott– S.Lott2009年10月16日 00:16:12 +00:00Commented Oct 16, 2009 at 0:16
-
The 'if' keyword must be lowercase. But this is a good catch that the syntax error highlighting caret points to 'aaa2' not the capital 'If', even in Python 3.12smci– smci2023年12月04日 03:46:12 +00:00Commented Dec 4, 2023 at 3:46
3 Answers 3
if must be written in lower case.
Furthermore,
- Write function names in lower case (see PEP 8, the Python style guide).
- Write the body of an
if-clause on a separate line. - Though in this case you'll probably not run into trouble, be careful with comparing floats for equality.
Since you've just started learning Python, you may want to get acquainted with writing parentheses around the arguments to
print, since from Python 3 onwards, print is a function, not a keyword.
To enforce this syntax in Python 2.6, you can put this at the top of your file:from __future__ import print_functionDemonstration:
>>> print 'test' test >>> from __future__ import print_function >>> print 'test' File "<stdin>", line 1 print 'test' ^ SyntaxError: invalid syntax >>> print('test') testFor more on
__future__imports, see the documentation.
2 Comments
It's the capital 'I' on "If". Change it to "if" and it will work.
Comments
How about
def Objects9(aaa1, aaa2):
if aaa2 != 0: print aaa1 / aaa2
Python keywords are case sensitive, so you must write 'if' instead of 'If', 'for' instead of 'fOR', et cetera.