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

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Unhappy Identation rules


    I am fairly new to Python programming. I thought I knew the rules for indentation but while coding I ran into an issue. I opened up my manual, Learning Python by Mark Lutz. The example below, from the book, does not work either. Running Python 2.6.6 on Linux.

    >>>
    >>> x=1
    >>> if x:
    ... y=2
    ... if y:
    ... print('block2')
    ... print('block1')
    ... print('block0')
    File "<stdin>", line 6
    print('block0')
    ^
    SyntaxError: invalid syntax
    >>>


    Any one know why?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Smile Answered my own question ... but ...


    Searching the forum, in Tips, tricks, etc., Dave - The Developers' Coach, gave a link to a Quick reference guide. There is a reference to:

    #!/usr/bin/env python
    # -*- coding: latin1 -*-

    Apparently without the above header in the module, the encoding is affected. It looks like it gets encoded to 7 bit ASCII if I am reading it right.

    Interestingly enough, in the interactive Python (cmdln), the above two lines need to be entered at start of the session for the code to run when coping and pasting code into the shell. Nowhere have I read this as I just figured this out. I hope this helps someone else who might be stuck.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2
    you do know you have to indent once pr if sentence you want things to be dependent on?

    is it this you are looking for?

    Code:
    x=1
    if x:
        y=2
        if y:
            print('block2')
            print('block1')
            print('block0')
    or this?
    Code:
    x=1
    if x:
        y=2
    if y:
        print('block2')
        print('block1')
        print('block0')
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    You did this:
    Code:
    >>> x=1
    >>> if x:
    ...     y=2
    ...     if y:
    ...         print('block2')
    ...     print('block1')
    ... print('block0')
      File "<stdin>", line 6
        print('block0')
            ^
    SyntaxError: invalid syntax
    you need to enter an empty line to return to block0.
    This is only true in the interpreter, not true of source code python reads from a file. Frankly, I don't know the interpreter rules. I edit source files for almost all my python work and import them. Here's an interpreter rule, python sets _ to the result of the last expression.
    Code:
    >>> 3+4
    7
    >>> _
    7
    >>>
    Anyway, you needed to enter your code into the interpreter as in this sample session:
    Code:
    $ python
    Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
    [GCC 4.7.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> x=1
    >>> if x:
    ...     y=2
    ...     if y:
    ...         print('block2')
    ...     print('block1')
    ... 
    block2
    block1
    >>> print('block0')
    block0
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo