[Pythonmac-SIG] raw_input() and input() with \n

Christopher Smith csmith@blakeschool.org
Wed, 08 May 2002 16:17:03 -0500


I just learned a tip from one of my students.  If you want a \n to be
included in a prompt provided to the raw_input or input functions you may
have been disappointed to find that it gets ignored and you end up with no
prompt in the dialog box at all.  Well...change those \n's to \r and now
it works!  Multiple lines will show up in the dialog window.

Here are two little replacement functions which also handle a cancel from
the user instead of raising the exception window:

#
# A workaround for the fact that \n causes the prompt to be lost b/c
# the buffer gets flushed before it gets to the SimpleDialog box.
# Thanks to Ryan Scheurer for this tip: replace \n wth \r

def iget(msg=''): #replacement for input
	try:
		return raw_input(msg.replace('\n','\r'))
	except EOFError: #user hit CANCEL
		return None
	except SyntaxError:  #user hit OK with nothing entered
		return None

def rget(msg=''): #replacement for raw_input
	try:
		return raw_input(msg.replace('\n','\r'))
	except EOFError: #user hit CANCEL
		return None


My 2.0/100,

/c