Development

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Reading character from keyboard

    1 answers - 1100 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Tommy Grav napisal(a):
    A very simple question. I would like to read a single character from the
    keyboard (y or n). I have tried to look in my Python books and a google
    search, but have come up empty. I am sure the info s out there, but I
    guess I am unable to find the right question or search keyword :o/
    Any hints or help appreciated
    Cheers
    Tommy
    Actually, if you want to read only one character from keyboard, you
    will need to use terminal operations rather than plain input stream.
    Unfortunately this is rather not portable solution. For Windows you
    have to import msvcrt, on Unices curses module. The latter code is
    quite similar. Example:
    import msvcrt
    def prompt(msg='Your choice: ', options={'y': True, 'n': False}):
    while True:
    print msg
    key = msvcrt.getch()
    choice = str(key).lower()
    if options.has_key(choice):
    break
    print 'You entered wrong key! Enter again.'
    return options[choice]
    print prompt() and 'Good for you.' or 'Bad Luck.'
  • No.1 | | 630 bytes | |

    At Tuesday 23/1/2007 15:28, consmash (AT) gmail (DOT) com wrote:

    A very simple question. I would like to read a single character from the
    keyboard (y or n). I have tried to look in my Python books and a google
    >
    >Actually, if you want to read only one character from keyboard, you
    >will need to use terminal operations rather than plain input stream.
    >Unfortunately this is rather not portable solution. For Windows you
    >have to import msvcrt, on Unices curses module. The latter code is


    There is a portable getch implementation, search the Python Cookbook.

Re: Reading character from keyboard


max 4000 letters.
Your nickname that display:
In order to stop the spam: 3 + 2 =
QUESTION ON "Development"

EMSDN.COM