[Tutor] elif statement

2010-08-10 Thread Sudarshana Banerjee
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:
 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

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.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] elif statement

2010-08-12 Thread Sudarshana Banerjee
Thank you Adam.

On Tue, Aug 10, 2010 at 7:03 PM, Adam Bark  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 "", 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@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] elif statement

2010-08-12 Thread Sudarshana Banerjee
Thank you Karim.. the code is not actually part of anything... but is a
textbook example, literally (Python Power by Matt Telles).

On Wed, Aug 11, 2010 at 9:59 AM, Karim  wrote:

>
> Hello,
>
> This code works for me:
>
>
> >>> x=3
> >>> if x==0:
> ... print "x is 0"
> ... elif x&1 ==1:
> ... print "x is a odd number"
> ... elif x&1==0:
> ... print "x is a even number"
> ...
>
> x is a odd number
>
> So I think you copied by error the string '-- Line 6' in you example given
> that has nothing to
> do with the rest of the code.
>
> Regards
> Karim
>
>
>
> On 08/11/2010 03:34 AM, Sudarshana Banerjee wrote:
>
>>print "x is a even number"
>>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] elif statement

2010-08-12 Thread Sudarshana Banerjee
Hi: Thank you very much for the detailed reply. My problem is I know I am
doing the indentation wrong; but I cannot get it right in IDLE.

Could you take a look at this please:
>>> x=3
>>> if x==0:
print "x is 0"

>>> elif x&1==1:
SyntaxError: invalid syntax

See, the moment I am pressing Enter the >>> is coming.. not ...

I am using IDLE on Mac; and this is driving me crazy! :) I can get the
result if I combine the if and print statements in one line.. but I really
want to know what is going on.

On Wed, Aug 11, 2010 at 1:22 AM, Dipo Elegbede wrote:

> You need to check the indentation properly.
> In this case, elif has to be on the same indentation level with if. I
> should think so.
> If you're working straight from the python interactive console, like I
> think you're doing, you need to manually do the indentation thing by
> yourself.
> First, I don't understand why you chose to set x to 3. That is not the
> main thing though.
> After x = 3, if you press enter, you'd get the python prompt
> >>>
> Then you type the next statement to have
> >>> if x == 0:
> What you get after this if statement is either a whitespace or ...,
> from my phone, I get a ...
> So you manually press the spacebar twice to get an indentation for the
> first print statement.
> As soon as you press enter again, you get the dots, just type in the
> elif statements without pressing the spacebar. Then press enter to
> move to a new line where the second print statement would be,and press
> the spacebar twice again for indentation.
> For the second elif statement, follow the procedure for the first elif
> statement.
> Go ahead and press enter twice to tell the console you are done, you
> shouldn't get an error that way.
> You should get something like below:
>
> >>> x=3
> >>> if x==0:
> ...   print x,'is zero'
> ... elif x//1==1:
> ...   print x,'is odd'
> ... elif x//1==0:
> ...   print x,'is even'
> ... else:
> ...   print'what is this'
> ...
>
> The else statement is optional.
> Hope it helps.
>
> On 8/11/10, Adam Bark  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 "", 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.
> >
> >
> >
>
>
> --
> Elegbede Muhammed Oladipupo
> OCA
> +2348077682428
> +2347042171716
> www.dudupay.com
> Mobile Banking Solutions | Transaction Processing | Enterprise
> Application Development
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] elif statement

2010-08-13 Thread Sudarshana Banerjee
Hi Alan: Yay! I pressed Enter after the print, and then delete. Which
brought the cursor back to the starting position at the beginning of the
line, and no more elif syntax errors. This is good to know. Thank you very
much.

I also took the liberty of checking out your computing website. It is really
helpful; and I have added it to my bookmarks.

Thanks a ton!

Sudarshana.

On Fri, Aug 13, 2010 at 1:27 AM, Alan Gauld wrote:

>
> "Sudarshana Banerjee"  wrote
>
>
>  Could you take a look at this please:
>>
>>> x=3
>>>>> if x==0:
>>>>>
>>>> print "x is 0"
>>
>>  elif x&1==1:
>>>>>
>>>> SyntaxError: invalid syntax
>>
>> See, the moment I am pressing Enter the >>> is coming.. not ...
>>
>
> IDLE doesn't give you a ... prompt it gives you spaces.
> So you mistake is that ypu are hitting Enter after your print line. It
> needs to look like:
>
>
>  x=3
>>>> if x==0:
>>>>
>>>   print "x is 0"
> elif x&1==1:
>   print
>
> Which looks horrible and I wish Idle were fixed so it didn't do this.
> I keep meaning to try and find a way to patch it myself!
>
>
> Essentially you need to ignore the >>> offset in the if line.
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor