my dog learned polymorphism*
The moose likes Jython/Python and the fly likes SyntaxError: invalid syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Jython/Python
Bookmark "SyntaxError: invalid syntax" Watch "SyntaxError: invalid syntax" New topic
Author

SyntaxError: invalid syntax

g Peshone
Ranch Hand

Joined: Oct 20, 2008
Posts: 44

Any suggestions why it's flagging?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31069
    
232

Are you copy/pasting the code from elsewhere?

This works:


This errors because I don't have a blank line after the if statement to let Python know it is over. In a script, I don't need the blank lone, but in the interpreter I do.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
g Peshone
Ranch Hand

Joined: Oct 20, 2008
Posts: 44
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
IPython 0.12.1 -- An enhanced Interactive Python


Python 3.2.3 (default, Sep 10 2012, 18:14:40)
[GCC 4.6.3] on linux2

I guess you know better why it is not working here. I just do not get the space here.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1873
    
  16

g Peshone wrote:Python 2.7.3 (default, Aug 1 2012, 05:14:39)
IPython 0.12.1 -- An enhanced Interactive Python


Python 3.2.3 (default, Sep 10 2012, 18:14:40)
[GCC 4.6.3] on linux2

I guess you know better why it is not working here. I just do not get the space here.

Python 3 requires parentheses for the print statement, but Python 2 doesn't. Try "print('Yes')" in Python 3.

With respect, you seem to be struggling even with the basic "Hello world" stuff here. It might be wise to take some time to read a little about how Python works, rather than hacking randomly in the hope of hitting the correct approach.


No more Blub for me, thank you, Vicar.
g Peshone
Ranch Hand

Joined: Oct 20, 2008
Posts: 44
Yes, it is working now. Wonder why they changed the syntax?
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1873
    
  16

g Peshone wrote:Yes, it is working now. Wonder why they changed the syntax?

One reason was apparently because the old print was a command, but the new print() is a function (http://www.linuxjournal.com/content/python-python-python-aka-python-3):
Linux Journal wrote:Of course, the obvious questions have to be asked: "Why introduce a change like this at all?" and "Why did the Python developers break most every existing Python program?".

It's possible to answer both of these questions with one answer: because it made sense to do so. The 2.x print command was always used as if it were a function, even though it was a command, which meant it was classed in with the likes of while, if, try, def and else, when it probably shouldn't have been. In Python, all function names have a trailing (), such as int(), input(), float(), range() and so on. As print was always more of a function than a command, it becomes print() in Python 3 which, of course, makes perfect sense, even though it breaks all that code!

They've also changed the formatted print from an old C-style approach to a more functional/flexible approach using string format templates and functions.
g Peshone
Ranch Hand

Joined: Oct 20, 2008
Posts: 44
well, I guess I need to do some extra reading.
Thanks much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SyntaxError: invalid syntax