Total Python Noob:为什么这不起作用?

发布于 2024-12-02 02:57:06 字数 1217 浏览 1 评论 0原文

我正在努力学习 Python the Hard Way,并试图理解它,而不是只是苦苦钻研。我陷入了练习 16,正如这里已经讨论的那样:

非常基本Python 问题(字符串、格式和转义符)

但我仍在试图找出为什么这种方法不起作用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-12-09 02:57:06
target.write("%s %s %s %s %s %s") % (line1, linebreak, line2, linebreak, line3, linebreak)

应该是

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

但最好写成:

target.write(' '.join(line1, linebreak, line2, linebreak, line3, linebreak))
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))
你穿错了嫁妆 2024-12-09 02:57:06

假设您得到的

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

是您需要的

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

,即您需要在字符串上使用 % 运算符,而不是在 target.write() 的结果上使用。如果您意识到 target.write() 返回 None(其类型为 NoneType),则该错误消息可能对您更有意义。

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.

数理化全能战士 2024-12-09 02:57:06

我几天前就这样做了,在练习 22 中,他会要求你写下到目前为止所学到的所有内容并记住它。

早些时候他要求你对每一行进行注释以解释它的作用。这可能是一个非常好的习惯,直到您无需考虑就知道这些线条的作用。

还要注意你陈述事情的方式。

如果你想成为一名程序员,你需要开始像程序员一样说话并使用词汇。

line1、line2、line3、linebreak 称为变量。您可以使用=(赋值运算符)临时给它们/分配它们值。

写是一个函数。 python 中的函数在 () 之间接受参数。 所有参数都必须位于 () 内。例如,如果你这样写,我确信它
不会让你绊倒的。

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

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
捂风挽笑 2024-12-09 02:57:06

我迟到了,但你也这样做:
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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文