0

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.

4
  • 2
    What python tutorial are you using to learn the language? Commented Oct 15, 2009 at 20:32
  • 1
    @S.Lott: :) I guess he hasn't followed your advice here stackoverflow.com/questions/1573548/… Commented 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. Commented 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.12 Commented Dec 4, 2023 at 3:46

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

the last point can be achieved by using from future import print_function on python 2.6
Good point, Bartosz. I updated the answer with some more information.
4

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.