Else statement executing when it shouldnt

Chris Angelico rosuav at gmail.com
Tue Jan 22 17:07:21 CET 2013


On Wed, Jan 23, 2013 at 2:48 AM, Thomas Boell <tboell at domain.invalid> wrote:
> On Wed, 23 Jan 2013 02:42:27 +1100
> Chris Angelico <rosuav at gmail.com> wrote:
>
>> On Wed, Jan 23, 2013 at 2:39 AM, Thomas Boell <tboell at domain.invalid> wrote:
>> > Huh?! I would have expected all your examples to raise a SyntaxError or
>> > IndentationError. Why don't they? Is 'else' not required to have a
>> > matching 'if'?
>>
>> Other things can have else, including 'for' and 'while' loops. :)
>
> I must say, that's bound to be confusing for anyone who knows any
> language other than Python (or none, even).  Syntax like that is "an
> accident waiting to happen"...

It can be confusing and does take that bit of learning, but it's SO
handy. In C code, I sometimes simulate it with a 'goto' and a comment,
but the actual keyword is much clearer.

ChrisA



More information about the Python-list mailing list