Reading character from keyboard

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tommy Grav

    #1

    Reading character from keyboard

    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
  • draghuram@gmail.com

    #2
    Re: Reading character from keyboard


    Tommy Grav 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
    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/
    You can use "raw_input" .



    If you are looking for a function to ask the user for confirmation, you
    can use something like this:

    -----------------------------------
    def confirm(_prompt =None, _default=False) :
    """prompts for yes or no response. Return True for yes and False
    for no."""
    promptstr = _prompt
    if (not promptstr):
    promptstr = "Confirm"

    if (_default):
    prompt = "%s [%s]|%s: " % (promptstr, "y", "n")
    else:
    prompt = "%s [%s]|%s: " % (promptstr, "n", "y")

    while (True):
    ans = raw_input(promp t)
    if (not ans):
    return _default
    if ((ans != "y") and (ans != "Y") and (ans != "n") and (ans !=
    "N")):
    print "please enter again y or n."
    continue
    if ((ans == "y") or (ans == "Y")):
    return True
    if ((ans == "n") or (ans == "N")):
    return False
    --------------------------------------------

    Usage:

    if (confirm("\nWan t to proceed?", _default=False) ):
    # proceed

    ------------------------------------------------

    Comment

    • consmash@gmail.com

      #3
      Re: Reading character from keyboard


      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='You r 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.'

      Comment

      • Gabriel Genellina

        #4
        Re: Reading character from keyboard

        At Tuesday 23/1/2007 15:28, consmash@gmail. 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.
        >Unfortunatel y 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.


        --
        Gabriel Genellina
        Softlab SRL






        _______________ _______________ _______________ _____
        Preguntá. Respondé. Descubrí.
        Todo lo que querías saber, y lo que ni imaginabas,
        está en Yahoo! Respuestas (Beta).
        ¡Probalo ya!


        Comment

        Working...