1

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.

Kevin
  • 74,910
  • 12
  • 133
  • 166
Dan
  • 9,935
  • 15
  • 56
  • 66
  • 2
    What python tutorial are you using to learn the language? – S.Lott Oct 15 '09 at 20:32
  • 1
    @S.Lott: :) I guess he hasn't followed your advice here http://stackoverflow.com/questions/1573548/python-def-function-how-do-you-specify-the-end-of-the-function – SilentGhost Oct 15 '09 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 Oct 16 '09 at 00:16

3 Answers3

16

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.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
Stephan202
  • 59,965
  • 13
  • 127
  • 133
4

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

steveha
  • 74,789
  • 21
  • 92
  • 117
3

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
  • 9,557
  • 2
  • 31
  • 39