[Tutor] Getting multi-line input from user
bob gailer
bgailer at gmail.com
Wed Jan 7 04:24:26 CET 2009
wormwood_3 wrote:
> Hello everyone,
>
> I'd like to prompt the user for input and get back multiple lines, for
> example if someone wanted to enter a few paragraphs. Then I'd like to
> be able to print their input out and preserve formatting. Here's what
> I have so far:
>
> control = True
> user_input = []
> while control:
> if not user_input:
> entry = raw_input("Enter text, 'done' on its own line to quit:
> \n")
> user_input.append(entry)
> else:
> entry = raw_input("")
> user_input.append(entry)
> if entry == "done":
> del user_input[-1]
> control = False
> user_input = ' '.join(user_input)
> print user_input
>
> So you end up with:
>
> Enter text, 'done' on its own line to quit:
> I am some text.
> And I am more.
>
> I am a new paragraph.
> done
> I am some text. And I am more. I am a new paragraph.
>
> 1) Is there a more elegant/common way to get multi-line user input
> than this sort of thing?
For starters you can simplify things a lot:
user_input = []
entry = raw_input("Enter text, 'done' on its own line to quit: \n")
while entry != "done":
user_input.append(entry)
entry = raw_input("")
user_input = ' '.join(user_input)
print user_input
> 2) How can I combine and print the output so that paragraphs and the
> like are preserved?
I don't understand. Please give an example.
--
Bob Gailer
Chapel Hill NC
919-636-4239
More information about the Tutor
mailing list