Very simple Python question (strings, formats, and screens)

I'm starting to learn Python with an online guide, and I just did the exercises that require me to write this script:

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

Everything worked out fine for me, but then the guide said: "There is too much repetition in this file. Use lines, formats and runs to print line1, line2 and line3 with only one target.write () command instead of 6".

I am not sure how to do this. Can anyone help? Thanks!

+6
source share
10 answers

The manual suggests creating one line and writing it, rather than calling write() six times, which seems like good advice.

You have three options.

You can concatenate the lines like this:

 line1 + "\n" + line2 + "\n" + line3 + "\n" 

or like this:

 "\n".join(line1,line2,line3) + "\n" 

To do this, you can use the old string formatting :

 "%s\n%s\n%s\n" % (line1,line2,line3) 

Finally, you can use the newer string formatting used in Python 3 and also available from Python 2.6:

 "{0}\n{1}\n{2}\n".format(line1,line2,line3) 

I would recommend using the latter method, because it is the most powerful when you hang it, which will give you:

 target.write("{0}\n{1}\n{2}\n".format(line1,line2,line3)) 
+16
source

What about

 target.write('%s \n %s \n %s' % (line1,line2,line3)) 
+5
source

I think they want you to use string concatenation:

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

Much less readable, but you only have one target.write() command

+1
source

This is done in two lines. It puts the line you want to print in the variable so that it is more readable.

 lb = "\n" allOnOne= line1 + lb + line2 + lb+ line3 + lb target.write(allOnOne) 
+1
source

I am currently following the same course, and the solution I found was similar to the one ninjagecko used, except that I used only what you were taught in the course so far. My looks like this:

 from sys import argv script, filename = argv print "We're going to erase %s." % 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." lines = [raw_input("Lines %r :" % i) for i in range(1, 4)] for line in lines: target.write(line + "\n") print "And finally, we close it." target.close() 

It took me a while to copy the parentheses and figure out where to put the formatter and loop, but as soon as I found it, it made sense to me. One important point is the first attempt:

 for i in range(1, 4): lines = raw_input("Line %r :" % i) 

It appears to work first when the script is run, however, when viewing the target file, it writes only the last line (line3) to the file. I still do not quite understand why this is so.

+1
source

I just take this course myself and for the first time I am wondering, and this is what I understood and received it without any problems. I am still involved in this, so if this is a bad form, let me know. This is what I have to work for me.

 target.write("%s \n%s \n%s" % (line1,line2,line3)) 
+1
source

For the purposes of β€œthis case study” question / problem in this specific guide, I believe that the author would like ...

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

Despite the fact that Dave Webb, of course, gets a lot of a lot of brown points for the fact that they were also useful and educational.

+1
source

badly

The source code is repeated, and the copy code is dangerous ( Why is copying and pasting code dangerous? ):

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

well

Significantly shorter, you can change it to 4+ lines by simply changing one character:

 print "Now I'm going to ask you for three lines." lines = [raw_input("line {i}: ".format(i=i)) for i in range(1,4)] print "I'm going to write these to the file." for line in lines: target.write(line+'\n') 
0
source

I think the goal was for the student to use what was taught in previous lessons and come to the following solution:

 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 + '\n' + line2 + '\n' + line3 + '\n') print "And finally, we close it." target.close() 
0
source

How about this? I used to loop.

 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 want that, hit RETURN.") input("?") print("Opening the file...") target = open(filename, 'w') print("Truncating the file. Goodbye!") target.truncate() print("Now I am going to ask you for three lines.") line1 = input("line 1: ") line2 = input("line 2: ") line3 = input("line 3: ") print("I'm going to write these to the file.") for a in (line1, line2, line3): target.write("\n") target.close() 
0
source

Source: https://habr.com/ru/post/890808/


All Articles