Alternative to raw_input ?

Nick Coghlan ncoghlan at iinet.net.au
Sat Feb 12 21:08:26 EST 2005


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   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list