469,926 Members | 1,558 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

raw_input on several lines

TP
Hi everybody,

When using raw_input(), the input of the user ends when he types Return on
his keyboard.
How can I change this behavior, so that another action is needed to stop the
input? For example, CTRL-G. It would allow the user to input several lines.

Thanks

Julien
--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
Aug 2 '08 #1
4 2197
TP wrote:
Hi everybody,

When using raw_input(), the input of the user ends when he types Return on
his keyboard.
How can I change this behavior, so that another action is needed to stop the
input? For example, CTRL-G. It would allow the user to input several lines.

Thanks

Julien

Just put raw_input() in a loop and check for something. Actually a blank line
would probably work best in that case. Not tested, but you will get the idea:

lines = list()
print 'Enter your text (empty line to quit)'
while 1:
line = raw_input('')
if line.strip() == '':
break

lines.append(line)

-Larry
Aug 2 '08 #2
How can I change this behavior, so that another action is needed to stop the
input? For example, CTRL-G. It would allow the user to input several lines.
I don't think you can change raw_input's behaviour in this respect, but
you could build something yourself that's based on interpretation of raw
keyboard scan codes.

Google is your friend in this...

e.g. on Linux you could use something like urwid
e.g. on win32 you could use something like
http://code.activestate.com/recipes/197140/

I am not aware of an os independent way to accomplish what you want.
Aug 2 '08 #3
TP wrote:
Hi everybody,

When using raw_input(), the input of the user ends when he types Return on
his keyboard.
How can I change this behavior, so that another action is needed to stop the
input? For example, CTRL-G. It would allow the user to input several lines.

Thanks

Julien
How about;

student_scores = {}
name = raw_input("Please enter a student name (q quits): ")
while(name != 'q'):
score = input("Please enter the students score: ")
student_scores[name] = score
name = raw_input("Please enter a student name (q quits): ")
for current_key in student_scores.keys():
print "The current students are:", current_key

--
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com

Aug 2 '08 #4
On Sat, 02 Aug 2008 21:58:09 +0200, TP wrote:
Hi everybody,

When using raw_input(), the input of the user ends when he types Return
on his keyboard.
How can I change this behavior, so that another action is needed to stop
the input? For example, CTRL-G. It would allow the user to input several
lines.

Thanks

Julien
Well I don't know about using CTRL-G. Now I'm pretty sure you can't
change the behavior of raw_input() like this *but* what you do is the
following:
>>from sys import stdin
user_input = stdin.readlines()
this
is
a multiline
test
>>user_input
['this\n', 'is\n', 'a multiline\n', 'test\n']
>>>
The end of the input is marked by the "End of Transmission" or "End of
File" character(which can be obtained via ctrl+D, at least on linux, I
have no idea about win32)
This would have the additional bonus of working with something being
piped into it as an input
Aug 3 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.