[Tutor] elif statement

Sudarshana Banerjee sudarshana.b at gmail.com
Fri Aug 13 03:39:03 CEST 2010


Thank you Adam.

On Tue, Aug 10, 2010 at 7:03 PM, Adam Bark <adam.jtm30 at gmail.com> wrote:

>  On 11/08/10 02:34, Sudarshana Banerjee wrote:
>
> Hi: I am trying to teach myself Python, and am stuck at the indentation
> with the elif statement.
>
> This is what I am trying to type (as copied from the textbook):
>
> x=3
> if x==0:
>     print "x is 0"
> elif x&1 ==1:
>     print "x is a odd number"
> elif x&1==0: -- Line 6
>     print "x is a even number"
>
> If I am combining the if and the print statement, then the elif statement
> is in the next line, and all is well with the world. If however, I write the
> print as a separate statement, I am getting a syntax error after I press
> Enter after keying the first elif statement.
>
>  >>> x=3
> >>> if x==0:
>  print x
>  elif x==2:
>
>
> Here you have indented the elif statement but it should be at the same
> level as the if:
>
> >>> x=3
> >>> if x==0:
> ...     print x
> ... elif x==2:
> ...     print "something else"
> ...
> >>>
>
>
>    SyntaxError: invalid syntax
>
>  Again:
>  >>> x=3
> >>> if x==2: print x
> elif x&1 == 1: print 'x is odd'
> >>> elif x&1 ==0: print 'x is even'
> SyntaxError: invalid syntax
>
>
> I'm not sure what's going on here but the second elif is being interpreted
> separate to the rest of the if statement hence a SyntaxError:
>
> >>> elif x&1 == 0: print "x is even"
>   File "<stdin>", line 1
>
>     elif x&1 == 0: print "x is even"
>        ^
> SyntaxError: invalid syntax
>
> This works:
>
> >>> if x==2: print x
> ... elif x&1 == 1: print 'x is odd'
> ... elif x&1 ==0: print 'x is even'
> ...
> x is odd
>
>
>
>
>  If I am pressing two Enters, the code executes; so I have a elif without
> a if, and again, a syntax error. What am I not doing right?
>
>  Thank you.
>
>  Sudarshana.
>
> HTH,
> Adam.
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100812/0261e016/attachment.html>


More information about the Tutor mailing list