473,324 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 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 2365
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....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.