please help me with my python code

A forum for general discussion of the Python programming language.

please help me with my python code

Postby spidernet » Thu Oct 31, 2013 2:42 pm

i am learning how to program in python. i have this code and i want to write all the target.write in one line. please help me


Code: Select all
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()
Last edited by Mekire on Thu Oct 31, 2013 3:02 pm, edited 1 time in total.
Reason: First post lock. Added code tags. Please read the post "New users read this"
spidernet
 
Posts: 1
Joined: Thu Oct 31, 2013 2:36 pm

Re: please help me with my python code

Postby stranac » Thu Oct 31, 2013 3:09 pm

Here are a few possible ways:
Code: Select all
target.writelines([line1, "\n", line2, "\n", line3, "\n"])
# or
target.write(line1 + "\n" + line2 + "\n" + line3 + "\n")
# or
target.write('%s\n%s\n%s\n' % (line1, line2, line3))
# or
target.write('{}\n{}\n{}\n'.format(line1, line2, line3))
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1143
Joined: Thu Feb 07, 2013 3:42 pm

Re: please help me with my python code

Postby Kebap » Thu Oct 31, 2013 3:45 pm

Code: Select all
lines = [line1, line2, line3]
target.write("\n".join(lines)
target.write("\n")
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: please help me with my python code

Postby ochichinyezaboombwa » Thu Oct 31, 2013 8:43 pm

A next step would be to get rid of three variables line1, line2, line3 and use only one call to raw_input(). It's a challenge! :-)
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: please help me with my python code

Postby Kebap » Fri Nov 01, 2013 9:16 pm

Unfortunately, spidernet last visited 20 minutes after registration, that is 10 minutes after writing the original post here. :|
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: please help me with my python code

Postby Marbelous » Wed Nov 06, 2013 11:32 pm

Maybe he got lost web crawling? :D
Marbelous
 
Posts: 141
Joined: Fri May 31, 2013 8:12 pm


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests

cron