开发者

syntax error on `If` line

My code:

#!/usr/bin/env python

def Runaaall(a开发者_高级运维aa):
  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.


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_function
    

    Demonstration:

    >>> print 'test'
    test
    >>> from __future__ import print_function
    >>> print 'test'
      File "<stdin>", line 1
        print 'test'
                   ^
    SyntaxError: invalid syntax
    >>> print('test')
    test
    

    For more on __future__ imports, see the documentation.


It's the capital 'I' on "If". Change it to "if" and it will work.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜