New assignmens ...
Antoon Pardon
antoon.pardon at vub.be
Thu Oct 28 04:04:11 EDT 2021
Op 27/10/2021 om 17:05 schreef Christman, Roger Graydon:
> I'm going to provide two loop-and-a-half segments to illustrate my interpretation
> of this PEP and the purpose of the walrus operator:
>
> [ first example ]
>
> Now contrast with this example:
>
> Without the walrus:
>
> replay = True
> while replay:
> play_game()
> replay = input("Play again? ") in ['y','Y','yes','Yes']
>
> (I think it silly to ask about playing again at first).
>
> With the walrus:
>
> replay = None
> while replay==None or (replay := input("Play again? ") in ['y','Y','yes','Yes']:
> play_game()
>
> To use the walrus operator here, I have to fabricate a value that would
> allow me to bypass the input operation, that cannot be otherwise produced.
> I do not find this second version any clearer or more intuitive than the first
> (and the PEP did emphasize the value of clarity).
But the above is not a one and a half loop. The above is essentially a do
loop (repeat loop in pascal), where you have the test at the end of the
loop body. But because python doesn't have that loop you have to use a
boolean to control the loop that is initialized to True and then later
assign that boolean the result of the test which you use to control this
loop.
Should I think it worth the trouble to rewrite your example, quod non,
it would be like below, with that unneeded list.
while [
play_game(),
input("Play again? ") in ['y', 'Y', 'yes', 'Yes']][-1]:
pass
--
Antoon Pardon.
More information about the Python-list
mailing list