How can I simplify this series of target.write commands?
<p>I'm working my way through Zed Shaw's "Learn Python The Hard Way". I'm up to exercise 16 ( <a href="http://learnpythonthehardway.org/book/ex16.html" rel="nofollow">http://learnpythonthehardway.org/book/ex16.html</a> ) and am running into a problem figuring out extra credit # 3. There are a series of 6 target.write commands towards the bottom of the script and Zed wants me to simplify them into a single target.write command using strings, formats and escapes.</p>

<p>Here is the original script with the 6 target.write commands...</p>

<pre><code>from sys import argv

script, filename = argv

print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."

raw_input("?")

print "Opening the file..."
target = open(filename, 'w')

print "Truncating the file.  Goodbye!"
target.truncate()

print "Now I'm going to ask you for three lines."

line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")

print "I'm going to write these to the file."

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print "And finally, we close it."
target.close()
</code></pre>

<p>First I tried condensing the target.write commands like this...</p>

<pre><code>target.write (line1, line2, line3)
</code></pre>

<p>When I run the script I get; TypeError: function takes exactly 1 argument (3 given)</p>

<p>Then I tried...</p>

<pre><code>target.write "I love %r and %r and %r." % (line1, line2, line3)
</code></pre>

<p>I get SyntaxError: invalid syntax</p>

<p>I also tried...</p>

<pre><code>target.write (line1), (line2), (line3)
</code></pre>

<p>This time the script ran to competition without any errors, but when I open the file that the script is supposed to write to (text.txt) it only wrote the first string (line1) into the file, but not the other two strings (line2) and (line3).</p>

<p>Finally, I tried this...</p>

<pre><code>target.write (line1, "\n", line2 "\n", line3, "\n")
</code></pre>

<p>But again I got an SyntaxError: invalid syntax</p>

<p>Can someone point me in the right direction on this?</p>

<p>Much appreciated.</p>

<p>Eddie</p>
