Thread: invalid syntax

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

    Join Date
    Apr 2004
    Posts
    2
    Rep Power
    0

    invalid syntax


    hey all i'm new member here and i have problem with python2.3
    i wanted to write this code for example
    h='hello'
    if 'n' in h:
    print ' n is in hello'
    else:
    print' n is not in hello'

    when i try to check or this code i got this message
    invalid syntax and it show else in red quarter
    although this is exmaple in the support.pdf file i'm newbie to python and i don't know what is wrong
    i hope i get helo soon
    bye
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    27
    Rep Power
    0
    Please post your code in CODE /CODE brackets when posting. You can do this by pressing the '#' button in the post editor. This will preserve any indentation you have in your code. Otherwise, indentation is lost. And that seems to be the only problem I can find with the code you gave: indentation. Indentation is a part of the Python syntax, so you MUST properly indent or else!

    Your code should look like this:

    Code:
    h = 'hello'
    if 'n' in h:
        print ' n in hello'
    else:
        print ' n is not in hello'
    Code:
    print' n is not in hello'
    Also be sure to put a space between print and your quotation. While this does not generate an error, it reduces readability.

    If this doesn't solve the problem, in your reply, also be sure to post a copy of your error report.

    Derrick
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    2
    Rep Power
    0
    thanks derrick for replying me
    but it still gives me this error invalid syntax and i don't know what is wrong i copied and paste the code which was in support.pdf file and it didn't work
    i tried to writie it in python shell and in python command line and still gives me this error
    Invalid syntax
    i guess i should download python 2.4 alpha maybe it won't show me the error
    thanks derrick again for ur tips
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Changing to 2.4 will not make any difference, since the problem is with your code not with Python.

    I don't know what the support.pdf file is since it is not part of standard Python, but cutting and pasting from a PDF file is likely to screw up the indentation, so that is probably the cause of your problem. Please read p4j's post again, since he has explained it once already. Try cutting and pasting the code he gave you, and you will see that it works fine.

    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo