raw_input on several lines

T

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)
 
D

David

TP said:
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
 
S

Stefaan Himpe

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.
 
R

Raja Baz

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:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top