Reading character from keyboard

T

Tommy Grav

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 :eek:/

Any hints or help appreciated
Cheers
Tommy
 
D

draghuram

Tommy said:
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 :eek:/

You can use "raw_input".

http://docs.python.org/lib/built-in-funcs.html

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(prompt)
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("\nWant to proceed?", _default=False)):
# proceed

------------------------------------------------
 
C

consmash

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 :eek:/

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.'
 
G

Gabriel Genellina

At said:
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.


--
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!
http://www.yahoo.com.ar/respuestas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top