Total Python Noob:为什么这不起作用?
我正在努力学习 Python the Hard Way,并试图理解它,而不是只是苦苦钻研。我陷入了练习 16,正如这里已经讨论的那样:
但我仍在试图找出为什么这种方法不起作用:
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()
我已经建立了换行符值,并且正在调用 line1、line2 和 linebreak 值在 target.write 命令中使用 %s。读取时不应该解析为“line1 \n line2 \n line3 \n”吗?
这可能相当于一个孩子问什么让天空保持向上之类的问题,我很抱歉我的语气有点厚重。谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
应该是
但最好写成:
should be
but would be better written as:
假设您得到的
是您需要的
,即您需要在字符串上使用
%
运算符,而不是在target.write()
的结果上使用。如果您意识到target.write()
返回None
(其类型为NoneType
),则该错误消息可能对您更有意义。Assuming that you're getting
What you need is
That is, you need to use the
%
operator on the string, not on the result oftarget.write()
. The error message might make more sense to you if you realise thattarget.write()
returnsNone
, which has typeNoneType
.我几天前就这样做了,在练习 22 中,他会要求你写下到目前为止所学到的所有内容并记住它。
早些时候他要求你对每一行进行注释以解释它的作用。这可能是一个非常好的习惯,直到您无需考虑就知道这些线条的作用。
还要注意你陈述事情的方式。
如果你想成为一名程序员,你需要开始像程序员一样说话并使用词汇。
line1、line2、line3、linebreak 称为变量。您可以使用=(赋值运算符)临时给它们/分配它们值。
写是一个函数。 python 中的函数在 () 之间接受参数。 所有参数都必须位于 () 内。例如,如果你这样写,我确信它
不会让你绊倒的。
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.
我迟到了,但你也这样做:
target.write("{line1}\n{line2}\n{line3}\n").format(line1=line1,line2=line2,line3=line3)
I'm very late but you, also do this:
target.write("{line1}\n{line2}\n{line3}\n").format(line1=line1,line2=line2,line3=line3)