Re: Simple if-else question

2009-10-01 Thread Olof Bjarnason
2009/10/1 Sion Arrowsmith : > MRAB   wrote: >>> [ for ... else ] >>The example that makes it clearest for me is searching through a list >>for a certain item and breaking out of the 'for' loop if I find it. If I >>get to the end of the list and still haven't broken out then I haven't >>found the it

Re: Simple if-else question

2009-10-01 Thread Sion Arrowsmith
MRAB wrote: >> [ for ... else ] >The example that makes it clearest for me is searching through a list >for a certain item and breaking out of the 'for' loop if I find it. If I >get to the end of the list and still haven't broken out then I haven't >found the item, and that's when the else statem

Re: Simple if-else question

2009-09-30 Thread alex23
dksr wrote: > Yes thats what I thought. for-else looks similar to if-else and in if- > else, else part is executed only when if part is not executed, but in > for-else it has entirely a different job. If you think of if-else more in terms of the else-branch occurring when the if-condition is no l

Re: Simple if-else question

2009-09-30 Thread Carl Banks
On Sep 30, 3:40 am, Iain King wrote: > Read the suggestion again - it's not a warning on the for-else > structure, it's a warning when the for-else doesn't contain a break; > he's theorising that a for-else without a break will always trigger > the else, in which case it's almost certainly an erro

Re: Simple if-else question

2009-09-30 Thread Duncan Booth
Iain King wrote: > However, I assume you can get past the else by raising an exception, > so the idea becomes a little muddled - do you warn when there is no > break and no explicit raise caught outside the loop? What about an > implicit exception? I would guess that code intentionally using an

Re: Simple if-else question

2009-09-30 Thread Iain King
On Sep 30, 7:12 am, Steven D'Aprano wrote: > On Tue, 29 Sep 2009 22:29:10 -0700, John Yeung wrote: > > On Sep 29, 1:15 pm, Carl Banks wrote: > >> Hmm, I wonder if Python should emit a warning if an else is used on a > >> for block with no break inside.  I don't think the else can be invoked > >>

Re: Simple if-else question

2009-09-30 Thread dksr
On Sep 29, 6:38 pm, Duncan Booth wrote: > Carl Banks wrote: > > Hmm, I wonder if Python should emit a warning if an else is used on a > > for block with no break inside.  I don't think the else can be invoked > > in any other way.  As a bonus it could catch some cases where people > > mistakenly

Re: Simple if-else question

2009-09-30 Thread Terry Reedy
John Yeung wrote: On Sep 29, 1:15 pm, Carl Banks wrote: Hmm, I wonder if Python should emit a warning if an else is used on a for block with no break inside. I don't think the else can be invoked in any other way. As a bonus it could catch some cases where people mistakenly use it thinking it

Re: Simple if-else question

2009-09-29 Thread Steven D'Aprano
On Tue, 29 Sep 2009 22:29:10 -0700, John Yeung wrote: > On Sep 29, 1:15 pm, Carl Banks wrote: >> Hmm, I wonder if Python should emit a warning if an else is used on a >> for block with no break inside.  I don't think the else can be invoked >> in any other way.  As a bonus it could catch some cas

Re: Simple if-else question

2009-09-29 Thread John Yeung
On Sep 29, 1:25 pm, MRAB wrote: > The example that makes it clearest for me is searching > through a list for a certain item and breaking out of > the 'for' loop if I find it. If I get to the end of the > list and still haven't broken out then I haven't found > the item, and that's when the else

Re: Simple if-else question

2009-09-29 Thread John Yeung
On Sep 29, 1:15 pm, Carl Banks wrote: > Hmm, I wonder if Python should emit a warning if an else is > used on a for block with no break inside.  I don't think the > else can be invoked in any other way.  As a bonus it could > catch some cases where people mistakenly use it thinking it > will execu

Re: Simple if-else question

2009-09-29 Thread Carl Banks
On Sep 29, 10:38 am, Duncan Booth wrote: > Carl Banks wrote: > > Hmm, I wonder if Python should emit a warning if an else is used on a > > for block with no break inside.  I don't think the else can be invoked > > in any other way.  As a bonus it could catch some cases where people > > mistakenly

Re: Simple if-else question

2009-09-29 Thread Duncan Booth
Carl Banks wrote: > Hmm, I wonder if Python should emit a warning if an else is used on a > for block with no break inside. I don't think the else can be invoked > in any other way. As a bonus it could catch some cases where people > mistakenly use it thinking it will execute when there are no

Re: Simple if-else question

2009-09-29 Thread MRAB
Ethan Furman wrote: Sandy wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i < 4:

Re: Simple if-else question

2009-09-29 Thread Carl Banks
On Sep 29, 9:08 am, Gary Herron wrote: > Sandy wrote: > > Hi all, > > A simple and silly if-else question. > > I saw some code that has the following structure. My question is why > > else is used there though removing else > > has the same result. More important, is it not syntactically wrong :-(

Re: Simple if-else question

2009-09-29 Thread Chris Kaynor
If I'm reading the indentation correctly, the else is applying to the for loop, not the if statement. When used in this way, the else occurs only if the for loop exits due to completion (aka, the for loop does not exit due to a break or return statement). I would expect the output from that code

Re: Simple if-else question

2009-09-29 Thread Ethan Furman
Sandy wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i < 4: print i else:

Re: Simple if-else question

2009-09-29 Thread Gary Herron
Sandy wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i < 4: print i else:

Re: Simple if-else question

2009-09-29 Thread Joel Goldstick
Sandy wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i < 4: print i else:

Simple if-else question

2009-09-29 Thread Sandy
Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i < 4: print i else: print i Chee