On Sun, Aug 6, 2017 at 1:23 PM, Mats Wichmann <m...@wichmann.us> wrote:

> Meanwhile, it is worth pointing out that while: (as with other python
> loops) can take an else: clause, which is executed if the loop runs to
> completion and was not exited via break.  That means you could ALSO
> write (this is pseudo-code for brevity):
>
> while guessesTaken < 6:
>     collect_guess
>     if guess_value == number
>         break
>     other_actions
> else:
>     print(guesses_ran_out)
>     quit_program
>
> # if we got here it's because a guess was correct
> print(congratulations)
>
> Using this is no more or less correct than not using it, just pointing
> it out for your learning about Python loops.  Apparently some people
> think while/else, for/else and so on are useless, or vile, or whatever.
> But they're certainly a part of the language.

Interesting.  I have seen else clauses for loops in the official
Python docs and books which try to be syntax-complete in their
presentation, but I cannot recall ever seeing this presented in
beginner and introduction books of which I have several [1].  Why is
this?  It does not seem a particularly difficult or confusing topic
and it can be useful.  Just wondering ...

[1]  An exception is "Beginning Python -- From Novice to Professional,
3rd ed." by Magnus Lie Hetland, c. 2017.  I recently acquired this
book and so far I am enjoying it.  I would not recommend it for a true
beginner to programming, but for anyone that has had any experience
programming in any language I think it might be a good read for such a
person to learn Python.  The author seems to bring in all of the
syntactic elements, either directly or in sidebars/sections that can
be skipped for later reading.  Perhaps unfortunately, he does not
provide many examples, thus making it fast-paced.  The latter section
of the book has ten substantial projects he goes through that have
mostly practical applications for real world examples, but if you have
any programming experience, this can be an advantage as such a person
probably does not need a lot of repetitive examples.  I might do a
more detailed review later once I finish the book to see if my initial
impression holds up.  But so far I wish it were the first Python book
I ever picked up!

-- 
boB
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to