[Tutor] Input

Ed Hughes bgeddy at gmail.com
Sat Oct 1 20:52:09 CEST 2011


Have a look at the Windows/Unix/Linux solution posted in the ActiveState
Python Recipes, (a fantastic resource),
here<http://code.activestate.com/recipes/134892/> -
a bit long winded maybe but it works fine under Linux and Windows for me.
It's pretty much based on the standard docs solution as already mentioned
for Linux/Unix. It use the tty and termios modules in Unix/Linux and the msvcrt
module in Windows, again, as already mentioned.

BgEddy

On 1 October 2011 11:00, <tutor-request at python.org> wrote:

> Send Tutor mailing list submissions to
>        tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>        http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>        tutor-request at python.org
>
> You can reach the person managing the list at
>        tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>   1. Re: Input (Steven D'Aprano)
>   2. Re: guess age programme (please help) (Steven D'Aprano)
>   3. fake defrag revisited (R. Alan Monroe)
>   4. Re: guess age programme (please help) (Alan Gauld)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sat, 01 Oct 2011 12:36:57 +1000
> From: Steven D'Aprano <steve at pearwood.info>
> To: tutor at python.org
> Subject: Re: [Tutor] Input
> Message-ID: <4E867CC9.3050500 at pearwood.info>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Cameron Macleod wrote:
> > Hi,
> >
> > When you type
> >
> > Input("\n\nPress The Enter Key To Exit")
> >
> > it forces you to press the enter key to close the program. Why is it the
> > enter key instead of e.g. the 'esc' key?
>
> Because the convention is that you use the Enter key to ENTER
> information. That's why it is called Enter.
>
> Your question is kind of like asking "Why do you use a screw driver for
> driving screws, instead of a socket wrench?" <wink>
>
> In all terminals I know of, the user's input is collected in a buffer
> until the Enter key is pressed. Until then, what the user types isn't
> available to the caller. This is how raw_input (Python 2) and input
> (Python 3) work.
>
> If you want to read the user's input character by character, as they are
> typed, it is actually quite tricky, but it can be done. You can install
> a full-blown GUI tool kit, like wxPython or Tkinter, and use that. Or
> you can use the curses module, which is a bit lighter than Tkinter but
> still pretty heavyweight. For Linux, there's a FAQ:
>
>
> http://docs.python.org/faq/library.html#how-do-i-get-a-single-keypress-at-a-time
>
> On Windows, you can use msvcrt.getch().
>
>
> --
> Steven
>
>
> ------------------------------
>
> Message: 2
> Date: Sat, 01 Oct 2011 14:13:43 +1000
> From: Steven D'Aprano <steve at pearwood.info>
> To: tutor at python.org
> Subject: Re: [Tutor] guess age programme (please help)
> Message-ID: <4E869377.5030503 at pearwood.info>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Hi Adrian,
>
> ADRIAN KELLY wrote:
> > Hi all,
> > can anyone help me with the attached programme.
>
> I'd love to, but I can't open it :(
>
> If the truth be known, I could open it if I spent the time and effort.
> But that actually is significant time and effort: in my email, your
> attachment looks like this:
>
> Content-Type: application/octet-stream
> Content-Transfer-Encoding: base64
> Content-Disposition: attachment; filename="guess age_game(broken)"
>
>
> DQpwcmludCgiXHRXZWxjb21lIHRvICdHdWVzcyBNeSBOdW1iZXInISIpDQpwcmludCgiSSdtIHRo
>
> aW5raW5nIG9mIGEgbnVtYmVyIGJldHdlZW4gMSBhbmQgMTAwLiIpDQpwcmludCgiVHJ5IHRvIGd1
>
> ZXNzIGl0IGluIGFzIGZldyBhdHRlbXB0cyBhcyBwb3NzaWJsZS5cbiIpDQoNCiMgc2V0IHRoZSBp
>
> bml0aWFsIHZhbHVlcw0KYWdlID0gMzUNCmd1ZXNzID0gIiAiDQp0cmllcyA9IDUNCg0KIyBndWVz
>
> c2luZyBsb29wDQp3aGlsZSBndWVzcyAhPSBhZ2U6DQogICAgDQogICAgZ3Vlc3MgPSBpbnB1dCgi
>
> VGFrZSBhIGd1ZXNzOiAiKQ0KICAgIGlmIGd1ZXNzID4gYWdlOg0KICAgICAgICBwcmludCAiTG93
>
> ZXIuLi4iLHRyaWVzLCJsZWZ0Ig0KICAgIGVsc2U6DQogICAgICAgIHByaW50ICJIaWdoZXIuLi4i
>
> LHRyaWVzLCJsZWZ0Ig0KICAgICAgICAgICAgDQogICAgdHJpZXM9dHJpZXMtMQ0KICAgDQoNCnBy
>
> aW50ICJcblxuWW91IGd1ZXNzZWQgaXQhICBUaGUgYWdlIHdhcyIsIGFnZSwiaXQgb25seSB0b29r
>
> IHlvdSAiK2B0cmllc2ArIiBhdHRlbXB0cyB0byBnZXQgaXQiDQoNCiAgDQppbnB1dCAoIlxuXG5Q
> cmVzcyB0aGUgZW50ZXIga2V5IHRvIGV4aXQuIikNCg==
>
>
> I hope you understand why I don't care enough to go to the time and
> effort of saving it to a file, decoding it, renaming it, and then
> finally reading it.
>
> You can make it easier for your readers (and therefore increase the
> number of people able and willing to help) if you do this:
>
> (1) For small amounts of code, just copy and paste it into your email.
> Make sure you send ordinary plain text. No HTML email or "rich text", as
> that will often break the formatting of the code.
>
> (2) For larger amounts, say, more than 50 lines, attach it as a file.
> Make sure the file has a file extension .txt or .py even if you are
> using a computer system that doesn't need file extensions. If .py,
> please make sure the file name is legal for Python: no spaces or
> brackets or parentheses or dashes.
>
>
> Thanks in advance,
>
>
>
> --
> Steven
>
>
> ------------------------------
>
> Message: 3
> Date: Sat, 1 Oct 2011 02:06:36 -0400
> From: "R. Alan Monroe" <amonroe at columbus.rr.com>
> To: tutor at python.org
> Subject: [Tutor] fake defrag revisited
> Message-ID: <145120072334.20111001020636 at columbus.rr.com>
> Content-Type: text/plain; charset=us-ascii
>
> I'm revisiting the fake defrag program I posted about a few months
> ago. The concept is basically a screensaver or light show where you
> can enjoy watching entropy being reversed as colored blocks order
> themselves visually.
>
> I set it aside for a while because it was too slow, but I finally came
> up with a better algorithm for the simulated file creation & deletion.
> So I can throroughly scramble my imaginary files.
>
> Now I want to come up with a simulated defragging, but I wish it for
> cosmetic reasons to visit the various areas of the drive in a way that
> appears random, to make it less boring.
>
> The fake "drive" is 64 blocks (shown as 8x8 grid onscreen) for test
> purposes.
>
> The files as-is (fragged):
> freelist:[1, 3, 6, 7, 9, 10, 11, 13, 14, 15, 16, 18, 22, 23, 25, 26, 27,
> 28, 30, 31, 32, 33, 34, 36, 38, 39, 40, 41, 42, 43, 44, 46, 47, 49, 52, 57,
> 58, 59, 60, 61, 62, 63]
> 1000: [45, 0]
> 1002: [56, 19, 5, 35, 8]
> 1014: [21, 54, 2, 20, 53, 17, 12, 4, 37, 48]
> 1013: [50, 51, 55, 24, 29]
>
> I can predict the arrangement of the files in the ideal end state
> (defragged) by sorting the filenames in ascending order then assign
> them blocks based on an incrementing number and the files' known
> sizes:
> freelist: [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
> 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
> 57, 58, 59, 60, 61, 62, 63]
> 1000: [0, 1]
> 1002: [2, 3, 4, 5, 6]
> 1013: [7, 8, 9, 10, 11]
> 1014: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
>
>
> Which gives this list of before and after blocks: i.e. block 0 is
> destined to live at 1 after defrag, block 1 is destined to live at 22,
> etc.
>
> [(0, 1), (1, 22), (2, 14), (3, 23), (4, 19), (5, 4), (6, 24), (7, 25),
> (8, 6), (9, 26), (10, 27), (11, 28), (12, 18), (13, 29), (14, 30),
> (15, 31), (16, 32), (17, 17), (18, 33), (19, 3), (20, 15), (21, 12),
> (22, 34), (23, 35), (24, 10), (25, 36), (26, 37), (27, 38 ), (28, 39),
> (29, 11), (30, 40), (31, 41), (32, 42), (33, 43), (34, 44), (35, 5),
> (36, 45), (37, 20), (38, 46), (39, 47), (40, 48), (41, 49), (42, 50),
> (43, 51), (44, 52), (45, 0), (46, 53), (47, 54), (48, 21), (49, 55),
> (50, 7), (51, 8), (52, 56), (53, 16), (54, 13), (55, 9), (56, 2), (57,
> 57), (58, 58), (59, 59), (60, 60), (61, 61), (62, 62), (63, 63)]
>
> I initially thought I could just do a random.shuffle on this list to
> achieve the cosmetic randomness, until I realized the real problem is
> magically determining the correct sequence in which to perform the
> moves without ruining a future move inadverently.
>
> If I move 0-to-1 first, I've now ruined the future 1-to-22 which ought
> to have taken place in advance.
>
> Is there a deterministic-yet-seemingly-random algorithm out there
> whose name I wasn't aware of to be able to google it?
>
> Alan
>
>
>
> ------------------------------
>
> Message: 4
> Date: Sat, 01 Oct 2011 09:15:15 +0100
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] guess age programme (please help)
> Message-ID: <j66i6k$ge1$1 at dough.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 01/10/11 05:13, Steven D'Aprano wrote:
>
> > (2) For larger amounts, say, more than 50 lines, attach it as a file.
>
> Or put it on one of the several public web pastebins.
> That will give us all syntax colouring and no ambiguity about
> indentation levels etc that often seems to occur via email.
>
> http://en.wikipedia.org/wiki/Comparison_of_pastebins
>
> provides a list...
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
> ------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 92, Issue 2
> ************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111001/d06d4c12/attachment-0001.html>


More information about the Tutor mailing list