on writing a while loop for rolling two dice

Avi Gross avigross at verizon.net
Mon Sep 6 20:11:41 EDT 2021


Let me add something, Stefan. Some people just want to get a job done. For
this person, he had a very specific need for a one-time project where the
rest of it was understood but one small step was confusing. Frankly, he
could have done it faster by opening a text editor on something like a CSV
file and flipped some (actual) three-sided coins while manually doing a cut
and past of lines into three new files and near the end, make sure the last
few only went into files with a missing row or two.

I have too often had people ask me for programming help and when I suggested
they learn how to do it or let me make a more careful piece of code that
checks for errors or will easily work under various changed conditions,
guess what I usually encounter? Many just want it DONE. They often
sheepishly come back some time later with some variant of the same need that
mysteriously does not work with code they asked to be designed to just work
in one case! Often they hack away at the code in odd ways first and then
come to me to have it fixed.

But there are costs to generality and creating functions with dozens of
optional arguments and that handle a wide variety of inputs and thus
constantly are checking what types they are working with and perhaps
converting to others or need to create objects with lots of dunders, can
also make it error prone.

Looking at our forever loop discussion, how often are the contents of the
loop written with multiple terminating parts within the loop body that get
complex? I mean lots of break or continue statements all over the place
with multi-part if statements. Some can be rewritten as a more standard loop
with a condition like "while (a < 5 && found_it == FALSE || ( ... )) ..."
and often in the body you need if statements that let you skip the rest if
found_it is true. It can be a mess either way. At times you need to consider
rewriting it from scratch. This especially happens as requirements keep
changing. Ages ago we had code that processed MTA headers and every time we
had a meeting of standards bodies, we kept adding ever more headers and
keywords that often required some existing code to also look at other
headers that now might be present as they might change what you did in
existing code locations. I eventually suggested a rewrite and it turned out
to be more compact now that we evaluated some things first, rather than
within many existing parts.

Has anyone mentioned the really stupid looking forever loop in languages
with the other style of for loop?

for ( ; ;)  

All that generally was,  was an initialization command before a while and so
on but here all three parts were null, so why use it?

And in the python version, has anyone made a generator that returned NULL or
the like so you can say uselessly:

for ( _ in forever() ) ...


-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On
Behalf Of Stefan Ram
Sent: Monday, September 6, 2021 12:34 PM
To: python-list at python.org
Subject: Re: on writing a while loop for rolling two dice

"Avi Gross" <avigross at verizon.net> writes:
>For some people the "while true" method seems reasonable but it has a 
>problem if the internal body does not have some guarantee of an exit. 
>And

  A programming error where the condition used does not
  express the intent of the programmer can happen with
  any kind of while loop.

>help explain to the reader what your intention is, if done properly. It 
>also can confuse if you put in "while 5 < 6" or other weird ways to say 
>do this indefinitely.

  If any reader cannot understand "while True:", it's not
  a problem with the source code.

  When someone writes "while True:", it is clearly his intention
  that the following indented code is repeated until it's exited
  by some means, which means is not the while condition.

>His solution was to repeat large sections of code, three times, with 
>some modification, for the cases where the remainder (modulo N) was 0, 1 or
2.

  This is typical of 

    - beginners who just do not know the means of the language
      to reduce redundancy yet,

    - less experience programmers, who know the language but
      still are not able to apply features to reduce redundancy
      always,

    - premature publication of unfinished code where the
      redundancy has not yet been reduced, or

    - programmers who do not have the personal means to climb to
      the level of abstraction needed to remove the redundancy
      in a specific case.

  And, I think you wrote that too, sometimes less smart code is
  more readable or maintainable. Maybe three very similar blocks
  are repeated literally because future modifications are expected
  that will make them less similar.


--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list