osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

The "loop and a half"


On 06/10/2017 14:11, Peter J. Holzer wrote:
> On 2017-10-06 12:38, bartc <bc at freeuk.com> wrote:
>> On 06/10/2017 12:51, Chris Angelico wrote:
>>> What you REALLY mean is that you can't see the point of an interactive
>>> sort command. It doesn't fit into your workflow. And that's fine. It's
>>> not something you'd use very often. There are other programs, however,
>>> that behave exactly the same whether used in batch mode or interactive
>>> mode, and where you would do exactly the same thing as I described -
>>> provide input, and hit Ctrl-D to mark that you're done.
>>
>> Examples?
>>
>> And is there any reason why you wouldn't use a text editor to capture
>> your input first? I can see myself noticing an error I'd made 10 lines
>> up, which is now too late to change, and I've still got 100 lines to go.
>> What to do?
>>
>> I just can't anyone wanting to use programs that work in the way you
>> suggest. Not outside of a student exercise (read N numbers and display
>> the average), where getting the input correct isn't so important.
> 
> I regularly use at least cat, wc and od this way (plus a few of my own
> utilities like utf8dump). I'm sure I've used sort this way, too, though
> rather rarely. I usually don't type the input but paste it in,

Exactly. Probably no one ever uses these programs with actual live input 
with the possibility of uncorrectable errors getting through.

So not a good reason to use that coding pattern to write programs which 
ARE designed for live, interactive input:

print ("Enter blank expression to quit.")

while 1:

     buffer=input("Expr: ")
     if buffer == "": break
     try:
         print (eval(buffer))
     except:
         print ("Error")

print ("Done")

-- 
bartc