#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Can't get print function to work


    I'm completely new to the Python language and am learning it from the book Violent Python. The book uses version 2.6.5 whilst I'm using 2.7.3 on Linux and from what I can see online the syntax changed and you now need to have brackets around it. The code im trying to get to work from the book is:

    Code:
    port = 21
    banner = "FreeFloat FTP Server"
    print "[+] Checking for "+banner+" on port "+str(port
    Which comes up with a syntax error, and when i try to run:

    Code:
    port = 21
    banner = "FreeFloat FTP Server"
    print ("[+] Checking for "+banner+" on port "+str(port)
    or

    Code:
    port = 21
    banner = "FreeFloat FTP Server"
    print ("[+] Checking for ("+banner+") on port "+str(port)
    Still comes up with a syntax error
    any help would be much appreciated, cheers.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    157
    Rep Power
    2
    Originally Posted by theRandy
    I'm completely new to the Python language and am learning it from the book Violent Python. The book uses version 2.6.5 whilst I'm using 2.7.3 on Linux and from what I can see online the syntax changed and you now need to have brackets around it. The code im trying to get to work from the book is:

    Code:
    port = 21
    banner = "FreeFloat FTP Server"
    print "[+] Checking for "+banner+" on port "+str(port
    Which comes up with a syntax error, and when i try to run:

    Code:
    port = 21
    banner = "FreeFloat FTP Server"
    print ("[+] Checking for "+banner+" on port "+str(port)
    or

    Code:
    port = 21
    banner = "FreeFloat FTP Server"
    print ("[+] Checking for ("+banner+") on port "+str(port)
    Still comes up with a syntax error
    any help would be much appreciated, cheers.
    The first way should work. It works for me:
    Code:
    >>> port=21
    >>> banner="FreeFloat FTP Server"
    >>> print "[+] Checking for "+banner+" on port "+str(port)
    [+] Checking for FreeFloat FTP Server on port 21
    The functional form, print(), is only for Python v3, I think. In v2.x print is still a statement. What exactly does the error message say?
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    All three of your variants lack one closing bracket. You can easily see it simply counting opening and closing brackets...
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    Both Python 2 and 3 versions work with the print() function, but as pointed out you have to match your opening and closing parenthesis.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0
    Ive tried it with and without the opening and closing brackets, a lot of different combinations. All of them come up with the syntax error. The error just says syntax error btw nothing else...
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    Ive tried it with and without the opening and closing brackets
    It looks you've tried it blindly, without concerning what you are writing.

    As I've told earlier - see, in all three of your examples here is misbalance of brackets. Three left and two right, or two left with one right.

    You can "try it with and without" for many years, but until you simply have equality between opening and closing brackets you will have no success.
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    These are brackets: []
    These are parentheses: ()
    These are braces: {}

    This is mismatched parenthesis: str(port
    This is mismatched parenthesis: (...str(port)
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    This funny thread give me an idea for adding new task at my programming education web-site:

    Matching Brackets (at codeabbey.com)

    Solution should tell which bracket sequences are correct and which are not.

    These are brackets: []
    These are parentheses: ()
    These are braces: {}
    Thanks for correction, though I used "brackets" as the collective noun for different kind of these punctuation marks. Like it is explained in wiki.

    However my English is poor enough, so I may be mistaken again... Please feel free to correct me once more if it is so
  16. #9
  17. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,008
    Rep Power
    2790
    Originally Posted by b49P23TIvg
    These are braces: {}
    Curly Brackets, thank you so much

    (Braces or Curly Brackets are both correct. It is merely preference. Although the term Braces does add a degree of separation and clarity.)
    Last edited by Winters; September 30th, 2013 at 04:20 AM.
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Originally Posted by docs.python.org
    2.1.6. Implicit line joining
    Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes. For example:
    http://docs.python.org/3/reference/l...line-structure
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo