473,379 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Alternative to raw_input ?

I need something like "Press any key to continue" code for my program.
Currently I use : raw_input("Press Enter to continue ") but it's lame.
Jul 18 '05 #1
16 5609
On Fri, 11 Feb 2005 17:26:04 +0100, BOOGIEMAN <BO*********@yahoo.com> wrote:
I need something like "Press any key to continue" code for my program.
Currently I use : raw_input("Press Enter to continue ") but it's lame.


Err, why?

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 18 '05 #2
On Fri, 11 Feb 2005 16:35:19 +0000, Simon Brunning wrote:
Err, why?


It looks to ugly this way. I want to press
any key without ENTER to continue
Jul 18 '05 #3
On 2005-02-11, BOOGIEMAN <BO*********@YAHOO.COM> wrote:
On Fri, 11 Feb 2005 16:35:19 +0000, Simon Brunning wrote:
Err, why?


It looks to ugly this way. I want to press
any key without ENTER to continue


Like somebody already said: use the WConio module.

Somebody already posted a link.

I suggest you go look at it.

--
Grant Edwards grante Yow! ... the MYSTERIANS
at are in here with my
visi.com CORDUROY SOAP DISH!!
Jul 18 '05 #4
den
BOOGIEMAN wrote:
On Fri, 11 Feb 2005 16:35:19 +0000, Simon Brunning wrote:

Err, why?

It looks to ugly this way. I want to press
any key without ENTER to continue


Did you try this:

import msvcrt
msvcrt.getch()
Jul 18 '05 #5
On Fri, Feb 11, 2005 at 05:37:19PM +0100, BOOGIEMAN wrote:
On Fri, 11 Feb 2005 16:35:19 +0000, Simon Brunning wrote:
Err, why?


It looks to ugly this way. I want to press
any key without ENTER to continue


read the documentation on readline.

Hmm! it says "Availability: Unix". Any particular reason? readline
should be fine on OSX and Win32

--
John Lenton (jo**@grulic.org.ar) -- Random fortune:
sugar daddy, n.:
A man who can afford to raise cain.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQFCDOLjgPqu395ykGsRAig6AJ9extU/bMx7GbBs1DGrWKkfAx8bhACgiE5w
jhda3sNi9vgHa7+x2vau4Z8=
=qbJW
-----END PGP SIGNATURE-----

Jul 18 '05 #6
Err, why?

It looks to ugly this way. I want to press
any key without ENTER to continue


How about modifying it to

raw_input("Press ENTER to continue ")

Skip
Jul 18 '05 #7
On Fri, 11 Feb 2005 18:00:08 +0100, den wrote:
Did you try this:

import msvcrt
msvcrt.getch()


Yes, that's what I need. Thank you.
BTW, sometimes program continues
without me pressing any button, why ?
Jul 18 '05 #8
BOOGIEMAN wrote:
On Fri, 11 Feb 2005 18:00:08 +0100, den wrote:

Did you try this:

import msvcrt
msvcrt.getch()

Yes, that's what I need. Thank you.
BTW, sometimes program continues
without me pressing any button, why ?


Probably because you had already hit a key earlier, and
it was still in the keyboard queue.

You can flush it first:

print prompt
while msvcrt.kbhit():
msvcrt.getch()
msvcrt.getch()

Pack that up in a subroutine and call it when
you need to pause and continue only when the user
has seen your prompt. Any previous keystrokes
will be discarded by the loop, then it will wait
for a new one to be hit.
-Peter
Jul 18 '05 #9
Skip Montanaro wrote:
How about modifying it to

raw_input("Press ENTER to continue ")


You want him to just capitalize ENTER in the current message?
--
Michael Hoffman
Jul 18 '05 #10
On Fri, 11 Feb 2005 21:38:47 -0500, Peter Hansen wrote:
print prompt
while msvcrt.kbhit():
msvcrt.getch()
msvcrt.getch()


Thanks, it works but without line "print prompt" and also
I'm not sure if I should put this function :

def cekaj():
while msvcrt.kbhit():
msvcrt.getch()
msvcrt.getch()

#Or this one, which sounds more logical according to help
#kbhit() - Return true if a keypress is waiting to be read.

def cekaj():
msvcrt.getch()
while msvcrt.kbhit():
msvcrt.getch()

It works both ways, not sure which one is right
Jul 18 '05 #11
BOOGIEMAN wrote:
On Fri, 11 Feb 2005 21:38:47 -0500, Peter Hansen wrote:

print prompt
while msvcrt.kbhit():
msvcrt.getch()
msvcrt.getch()

Thanks, it works but without line "print prompt" and also
I'm not sure if I should put this function :

def cekaj():
while msvcrt.kbhit():
msvcrt.getch()
msvcrt.getch()

#Or this one, which sounds more logical according to help
#kbhit() - Return true if a keypress is waiting to be read.

def cekaj():
msvcrt.getch()
while msvcrt.kbhit():
msvcrt.getch()

It works both ways, not sure which one is right


Try this:

print "Hit a key!"
cekaj()
print "Nap time!"
time.sleep(15)
print "Hit another key!"
cekaj()

with the two different implementations, and see what happens if you hit a key
when the 'Nap Time!' prompt appears.

Cheers,
Nick.

P.S. You probably actually want an implementation that looks like:

def cekaj(prompt="Press a key to continue"):
while msvcrt.kbhit():
msvcrt.getch()
if prompt is not None:
print prompt
msvcrt.getch()

And the sample program would look like:
cekaj("Hit a key!")
print "Nap time!"
time.sleep(15)
cekaj("Hit another key!")

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #12
BOOGIEMAN wrote:
On Fri, 11 Feb 2005 21:38:47 -0500, Peter Hansen wrote:
print prompt
while msvcrt.kbhit():
msvcrt.getch()
msvcrt.getch()

Thanks, it works but without line "print prompt" and also


That was intended to be a hint that you might need
to print a prompt to the user (maybe a string contained
in a variable named "prompt", for example ;-), or the
program might halt without apparent reason.
I'm not sure if I should put this function :

def cekaj():
while msvcrt.kbhit():
msvcrt.getch()
msvcrt.getch()

#Or this one, which sounds more logical according to help
#kbhit() - Return true if a keypress is waiting to be read.

def cekaj():
msvcrt.getch()
while msvcrt.kbhit():
msvcrt.getch()

It works both ways, not sure which one is right


The point of doing the first approach is that you are
reading *all* available keystrokes (in the loop), and
then sitting and waiting for one more keystroke before
continuing. If you don't read all keystrokes first,
then if the user has hit a key, say, five minutes
earlier, it will still be sitting in the queue and
your program will not actually stop at all.

Your second approach sits and waits (if necessary)
to read a single keystroke, and then if the user had
managed to hit two or more keys** before that, it will
consume all remaining keystrokes before continuing.
Not exactly the same thing, nor generally quite
what you want. Based on what you originally asked for,
the former is the correct approach.

** Note that some keys will result in a pair of
values being retrieved by getch(), with the first one
being (generally... maybe always... I can't remember
but it's easy for you to experiment) a 0, and the second
being another code that identifies the key. The F5
key, for example, returns a 0 and then a 64 on the
second call to getch(). If you *really* want *any*
key to continue, you'll want to check for this sort
of thing as well... though personally I'd just use
raw_input() and keep my program portable. <wink>

-Peter
Jul 18 '05 #13
On Sun, 13 Feb 2005 12:08:26 +1000, Nick Coghlan wrote:
Try this:

print "Hit a key!"
cekaj()
print "Nap time!"
time.sleep(15)
print "Hit another key!"
cekaj()

with the two different implementations, and see what happens if you hit a key
when the 'Nap Time!' prompt appears.


I see the difference now, thanks
Jul 18 '05 #14
Le vendredi 11 Février 2005 18:00, den a écrit*:
import msvcrt
msvcrt.getch()


I frequently had the problem to have something similar but *portable*.
Something as short and simple.

Someone have an idea ?

Thank you

Francis Girard

Jul 18 '05 #15
Then may I suggest the keeping-it-simple approach:

def myGetch():
.....raw_input("Press Enter to continue")
(sorry about the dots, I'm using google groups)

Lars

Jul 18 '05 #16
Francis Girard wrote:
[den]:
import msvcrt
msvcrt.getch()


I frequently had the problem to have something similar but *portable*.
Something as short and simple.


This has been brought up many times on this newsgroup. The answer is that
there is no simple way to do this portably. You could use Python's
exception handling to create a version that runs on multiple systems
though, falling back to another method if msvcrt can't be imported.
--
Michael Hoffman
Jul 18 '05 #17

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

Similar topics

1
by: Hugh | last post by:
I am using python 2.3 through the PythonWin program on windows. I would like to create a console-based interactive session. The program raw_input is almost exactly what I'd like, except that...
5
by: Helmut Jarausch | last post by:
Hi when using an interactive Python script, I'd like the prompt given by raw_input to go to stderr since stdout is redirected to a file. How can I change this (and suggest making this the...
2
by: J. W. McCall | last post by:
I'm working on a MUD server and I have a thread that gets keyboard input so that you can enter commands from the command line while it's in its main server loop. Everything works fine except that...
0
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run...
1
by: JerryKreps | last post by:
Hi, folks -- I'm a Python pup. As you can see from the session copied at the end of this post, I have the latest version of Python, and I've been using the Editor-Shell of the latest version of...
21
by: planetthoughtful | last post by:
Hi All, As always, my posts come with a 'Warning: Newbie lies ahead!' disclaimer... I'm wondering if it's possible, using raw_input(), to provide a 'default' value with the prompt? I would...
17
by: Stuart McGraw | last post by:
In the announcement for Python-2.3 http://groups.google.com/group/comp.lang.python/msg/287e94d9fe25388d?hl=en it says "raw_input(): can now return Unicode objects". But I didn't see anything...
7
by: Mike Kent | last post by:
It's often useful for debugging to print something to stderr, and to route the error output to a file using '2>filename' on the command line. However, when I try that with a python script, all...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.