473,473 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Jan 23 '07 #1
3 5767

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".

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

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

Jan 23 '07 #2

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

Jan 23 '07 #3
At Tuesday 23/1/2007 15:28, co******@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.
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

Jan 23 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: siliconmike | last post by:
All I know is that there are 8 bit numbers from 0 to 255 mapped to characters like A, B, C, D and some strange looking ones (like the ones used to make boxes in old PC text modes) all these being...
50
by: The Bicycling Guitarist | last post by:
A browser conforming to HTML 4.0 is required to recognize &#number; notations. If I use XHTML 1.0 and charset UTF-8 though, does é have as much support as é ? Sometimes when I run...
3
by: AA Arens | last post by:
When I want the first character of a field to be Uppercased, I need to make an input mask, like >L< followed by ??????? for example. But this mask creates ____ in an unfilled field, which I don't...
21
by: aegis | last post by:
7.4#1 states The header <ctype.h> declares several functions useful for classifying and mapping characters.166) In all cases the argument is an int, the value of which shall be representable as an...
40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
7
by: Drew Berkemeyer | last post by:
Hello, I'm using the following code to read a text file in VB.NET. Dim sr As StreamReader = File.OpenText(strFilePath) Dim input As String = sr.ReadLine() While Not input Is Nothing...
0
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as...
2
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as...
3
by: NaN | last post by:
Hi I'm using Dev-C++. Here is my sourcecode. /* GETCH.C: This program reads characters from * the keyboard until it receives a 'Y' or 'y'. */ #include <conio.h>
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.