Very basic Python question (strings, formats and escapes)

后端 未结 10 2076
别那么骄傲
别那么骄傲 2021-02-06 10:59

I am starting to learn Python with an online guide, and I just did an exercise that required me to write this script:

from sys import argv

script, filename = ar         


        
相关标签:
10条回答
  • 2021-02-06 11:39

    I am just taking this course my self too for the first time and was wondering the same thing and this is what I figured out and got it to work with no issues. I am still learning this so if this is bad form let me know. Here is what I got to work for me.,

    target.write("%s \n%s \n%s" % (line1,line2,line3))
    
    0 讨论(0)
  • 2021-02-06 11:44

    I am currently following the same course, and the solution I found was similar to the one ninjagecko used, except I only used the things you were taught up to this point in the course. Mine 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 awhile moving the parenthesis around and figuring out where to place the formatter and loop, but once I found it, it made perfect sense to me. One key thing to note is that my first attempt:

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

    Appears to work at first when you run the script, however when you review the target file it only writes the last line (line3) to the file. I'm still not entirely clear on why that is the case.

    0 讨论(0)
  • 2021-02-06 11:47

    How about

    target.write('%s \n %s \n %s' % (line1,line2,line3))
    
    0 讨论(0)
  • 2021-02-06 11:50

    This does it in two lines. It puts the line you want to print in a variable so it's more readable

    lb = "\n"
    allOnOne= line1 + lb + line2 + lb+ line3 + lb 
    target.write(allOnOne) 
    
    0 讨论(0)
  • 2021-02-06 11:55

    For the purposes of "that specific study drill" question/problem in that specific guide, i believe the author would like...

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

    Although, Dave Webb certainly gets many many browny points for being thorough plus educational value too.

    0 讨论(0)
  • 2021-02-06 11:57

    I think the intent was for the student to leverage what had been taught in previous lessons and arrive at the following as a 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 讨论(0)
提交回复
热议问题