for-else

Tim Chase python.list at tim.thechases.com
Tue Mar 4 11:00:06 EST 2008


> For instance, if you have a (trivial) if...elif...else like this:
> 
> if a == 0:
>     do_task_0()
> elif a == 1:
>     do_task_1()
> elif a == 2:
>     do_task_2()
> else:
>     do_default_task()
> 
> You could roll it up into a for...else statement like this:
> 
> for i in range(3):
>     if a == i:
>         do_task[a]()

important "break" missing here...

> else:
>     do_default_task()


or otherwise this code will do_task_i *and* do_default_task()...

-tkc





More information about the Python-list mailing list