The "loop and a half"

bartc bc at freeuk.com
Thu Oct 5 06:56:59 EDT 2017


On 05/10/2017 07:57, Gregory Ewing wrote:
> Steve D'Aprano wrote:
>> I recall that the Pascal compiler had to do some clever behind the scenes
>> jiggery-pokery to get eof() to work, but that's what compilers are 
>> supposed
>> to do: make common tasks easy for the programmer.
>> Sometimes the jiggery-pokery worked, sometimes it didn't.
> For example, the following wouldn't work as expected:
>>    while not eof(input) do begin
>       write(output, 'Enter something:');
>       readln(input, buffer);
>       process(buffer);
>    end
>> because the eof() would block waiting for you to enter
> something, so the prompt wouldn't get printed at the
> right time.
>> Basically, Pascal's eof-flag model was designed for
> batch processing, and didn't work very well for
> interactive use.

This doesn't make sense. For interactive use, you wouldn't bother 
testing for eof, as you'd be testing the eof status of the keyboard.
You might want a way of the user indicating end-of-data, but that's 
different; you don't want to abruptly send an EOF (via Ctrl-C, D, Z, 
Break or whatever). That would be a crass way of doing it. Besides you 
might want to continue interacting with the next part of the program.
So the loop would be like this (Python 3; don't know why it doesn't work 
in Python 2):
 while 1:
 buffer = input("Enter something (Type quit to finish): ")
 if buffer == "quit": break
 print ("You typed:", buffer) # process(buffer)
 print ("Bye")
This is a loop-and-a-half.
-- 
bartc


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /