Why doesn't Python include non-blocking keyboard input function?
BartC
bc at freeuk.com
Sat Oct 29 10:32:16 EDT 2016
On 29/10/2016 14:51, Dennis Lee Bieber wrote:
> On Sat, 29 Oct 2016 12:53:35 +0100, BartC <bc at freeuk.com> declaimed the
> following:
>
>>
>> BTW what does reading three integers from the user look like in Python?
>
> On one line, or on three lines?
>
> (Python 2.7)
>
> ln = raw_input("Enter three integers separated by spaces: ")
> ints = []
> for i, wd in enumerate(ln.split()):
> try:
> anInt = int(wd)
> except: #yes, I know -- the horrible bare except clause
> print ("***** item %s: '%s' could not be converted to integer"
> % (i, wd) )
> anInt = None
> ints.append(anInt)
Yes, that's the sort of thing I was expecting, that's why I asked.
I still think a beginner would much prefer something along the lines of
'readln a,b,c' (and I still think that's more intuitive).
(The first programming exercises I ever did involved typing in integers
from the user, and sorting them or working out if they made a triangle
or whatever.
But in Python someone would first need to master for-loops, enumeration,
string-processing, numeric conversion, exceptions and, your second
example, functions. That's quite a steep learning curve! (And that
second example starts to involve re-inventing things.)
(BTW the functionality of my 'readln a,b,c' differs from the above.
Separators can be anything reasonable. When eol is encountered, it will
read zeros. And errors are not handled: any non-numeric will yield zero.
When reading name, file and string items rather than integers, the split
method doesn't deal with embedded quotes. Probably there are bigger guns
you can bring out to deal with more elaborate input, but it starts to
get further away from beginner level.)
--
Bartc
More information about the Python-list
mailing list