tags:

views:

180

answers:

3

I'm not sure if this site is for dumb troubleshooting questions, but I figure this will take someone 2 seconds. 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.

Thanks

+14  A: 

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.

Stephan202
the last point can be achieved by using from __future__ import print_function on python 2.6
Bartosz Radaczyński
Good point, Bartosz. I updated the answer with some more information.
Stephan202
+3  A: 

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.

David Seiler
+4  A: 

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

steveha