Iteration, while loop, and for loop
jfong at ms4.hinet.net
jfong at ms4.hinet.net
Thu Jun 30 22:12:33 EDT 2016
Steven D'Aprano at 2016/6/30 7:59:40AM wrote:
> py> mi = list('bananas')
> py> for char in mi:
> ... if char == 'a':
> ... mi.extend(' yum')
> ... print(char, end='')
> ... else: # oh no, the feared for...else!
> ... # needed to prevent the prompt overwriting the output
> ... print()
> ...
> bananas yum yum yum
> py>
>
>
> This example shows two things:
...
> (2) Anyone who says that for...else without break is useless is wrong.
Haha...you win.
By the way, I never said "else" without "break" is illegal or useless, I said it will cause confusion semantically, just like this(it reminds me the IOCCC:-):
import math
pass
import os
pass
import sys
Think of the "try ...except ...else ..." where the situation is similar but luckily it was mandatory.
--Jach
More information about the Python-list
mailing list