472,371 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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 2298
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: planetthoughtful | last post by:
Hi All, As always, my posts come with a 'Warning: Newbie lies ahead!' disclaimer... I'm wondering if it's possible, using raw_input(), to provide a 'default' value with the prompt? I would...
4
by: BartlebyScrivener | last post by:
Using Python on Debian Etch. What is the best way to paste a block of text in at the command prompt. I'm trying something like: Quote = raw_input("Paste quote here: ") Which works great...
7
by: Mike Kent | last post by:
It's often useful for debugging to print something to stderr, and to route the error output to a file using '2>filename' on the command line. However, when I try that with a python script, all...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.