beginner question (True False help)

Chris Angelico rosuav at gmail.com
Thu Aug 8 07:41:50 EDT 2013


On Thu, Aug 8, 2013 at 7:20 AM,  <wxjmfauth at gmail.com> wrote:
>>>> def z2():
> ...     letters = 'abc'
> ...     while True:
> ...         c = input('letter: ')
> ...         if c not in letters:
> ...             print('end, fin, Schluss')
> ...             break
> ...         else:
> ...             print('do stuff')


Minor quibble: I don't like having a hard exit followed by an "else".
If the "if" branch will unconditionally quit the loop (with a break,
here, but could also be a return, a thrown exception, etc etc), I
would prefer to see the "else" removed and its code unindented one
level. Maybe this is just personal preference, though, learned from
assembly language programming where a "block if" looks something like
this:

; if x == y:
CMP x,y
JNZ .else
; Code for "x == y"
JMP .endif
.else:
; Code for "else"
.endif

Putting an unconditional departure in the "x == y" branch makes the
"JMP .endif" redundant.

ChrisA



More information about the Python-list mailing list