开发者

Total Python Noob: why doesn't this work?

I'm working through Learn Python the Hard Way, and trying to understand it rather than just hammer away. I got stuck on Exercise 16, as开发者_如何学运维 discussed already on SO here:

Very basic Python question (strings, formats and escapes)

but I'm still trying to figure out why this approach does not work:

from sys import argv

script, filename = argv


print "Attempting to open the file now." 
print open(filename).read()

print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-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." 

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

target.write("the ending line")

print "And finally, we close it." 
target.close()

I've established a value for linebreak, and am calling the line1, line2 and linebreak values with %s in the target.write command. Should't it parse as "line1 \n line2 \n line3 \n" when it's read?

This is probably the equivalent of being asked by a child what keeps the sky up or something, and I apologize for being kind of thick. Thanks!


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

should be

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

but would be better written as:

target.write(' '.join(line1, linebreak, line2, linebreak, line3, linebreak))


Assuming that you're getting

TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'

What you need is

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

That is, you need to use the % operator on the string, not on the result of target.write(). The error message might make more sense to you if you realise that target.write() returns None, which has type NoneType.


I did this a couple days ago, in exercise 22 he will ask you to write down everything you have learned so far and memorize it.

Earlier he asked that you comment every line to explain what it does. That is probably a really good habit until you just know what the lines do without having to think about them.

Also be careful about how you state things.

If you want to become a programmer you need to start talking like one and using the vocabulary.

line1, line2, line3, linebreak are called variables. You temporarily give them/assign them values using the = (assignment operator).

Write is a function. Functions in python take arguments in between (). All of the arguments have to fit inside the (). If you wrote it like this for instance I'm sure it wouldn't have tripped you up.

stuffIWantToPrint = line1 + lb + line2 + lb+ line3 + lb #all strings together 
target.write(stuffIWantToPrint) #pass the big string to write


I'm very late but you, also do this: target.write("{line1}\n{line2}\n{line3}\n").format(line1=line1,line2=line2,line3=line3)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜