It would help to know what operating system you're using, as this is a very operating-system-specific question. For example, Kylar's answer doesn't work on Windows because sys.stdin doesn't have a fileno attribute.
I was curious and threw together a solution using curses, but this won't work on Windows either:
#!/usr/bin/python
import time
import sys
import curses
def alarmloop(stdscr):
stdscr.addstr("How many seconds (alarm1)? ")
curses.echo()
alarm1 = int(stdscr.getstr())
while (1):
time.sleep(alarm1)
curses.flushinp()
stdscr.clear()
stdscr.addstr("Alarm1
")
stdscr.addstr("Continue (Y/N)?[Y]:")
doit = stdscr.getch()
stdscr.addstr("
")
stdscr.addstr("Input "+chr(doit)+"
")
stdscr.refresh()
if doit == ord('N') or doit == ord('n'):
stdscr.addstr("Exiting.....
")
break
curses.wrapper(alarmloop)
EDIT: ah, Windows. Then you can use the msvcrt module. Note that the code below isn't perfect, and it doesn't work in IDLE at all:
#!/usr/bin/python
import time
import subprocess
import sys
import msvcrt
alarm1 = int(raw_input("How many seconds (alarm1)? "))
while (1):
time.sleep(alarm1)
print "Alarm1"
sys.stdout.flush()
# Try to flush the buffer
while msvcrt.kbhit():
msvcrt.getch()
print "Continue (Y/N)?[Y]"
doit = msvcrt.getch()
print "Input",doit
if doit == 'N' or doit=='n':
print "Exiting....."
break
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…