[Tutor] How to print out multiple lines

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 7 Jan 2001 14:05:15 -0800 (PST)

> servername=input("SMTP server name: ")
> Fromaddress=input("From address: ")
> Toaddress=input("To address: ")
> MessageText=input("message text: ")
> server=smtplib.SMTP(servername)
> server.sendmail(Fromaddress, Toaddress, MessageText)
> server.quit()

You may want to use raw_input() instead of input(): input() acts like the
interpreter, so with it, you'll need to quote your strings.  I'm quite
sure that can get a bit annoying.  raw_input(), on the other hand, always
assumes that you want to get strings, so it doesn't require quotes.

To read multiple lines of text... hmmm... there are several ways to do
this.  How should the program know when to stop reading lines?  input()
and raw_input() are set up only to work on single lines, so you could
write a specialty inputting function instead that stops when it sees a
certain "terminating" line.

def readManyLines():
    """Read lines from the user until they enter a line with a single
period, and return a single string."""
    result = ""
    while 1:
        line = raw_input()
        if line == '.': return result
        result = result + line

Here's an example run:

>>> msg = readManyLines()
This is a test
of the emergency
broadcast system.
>>> msg   
'This is a testof the emergencybroadcast system.'
>>> print msg
This is a testof the emergencybroadcast system.

Hmmm... that's a bug!  I needed to add newlines between the
concatentations.  I think you should be able to fix this ok.

Hope this helps!