Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8279981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:30:31+00:00 2026-06-08T09:30:31+00:00

I’m working my way through Zed Shaw’s Learn Python The Hard Way. I’m up

  • 0

I’m working my way through Zed Shaw’s “Learn Python The Hard Way”. I’m up to exercise 16 ( http://learnpythonthehardway.org/book/ex16.html ) 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.

Here is the original script with the 6 target.write commands…

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()

First I tried condensing the target.write commands like this…

target.write (line1, line2, line3)

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

Then I tried…

target.write "I love %r and %r and %r." % (line1, line2, line3)

I get SyntaxError: invalid syntax

I also tried…

target.write (line1), (line2), (line3)

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).

Finally, I tried this…

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

But again I got an SyntaxError: invalid syntax

Can someone point me in the right direction on this?

Much appreciated.

Eddie

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:30:33+00:00Added an answer on June 8, 2026 at 9:30 am

    You could do

    target.write('\n'.join((line1,line2,line3))+'\n')
    

    Or, maybe written a little more clearly:

    lines=(line1,line2,line3)
    target.write( '\n'.join(lines) + '\n')
    

    Although, in this case, I might actually write the last newline separately to avoid the overhead of creating an entirely new string just to add on a newline at the end (and I think it looks cleaner):

    lines=(line1, line2, line3)
    target.write('\n'.join(lines))
    target.write('\n')
    

    This takes your lines, packs them together as a tuple and uses the join method of string objects to make them into a single string which gets written.

    The advantage to this approach (as opposed to string formatting) is that you don’t need to know a-priori how many “lines” you are going to write. Any iterable object will do work in place of the tuple in the above expression.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.