打印空线?
我正在关注有关Python的初学者教程,有一个小练习,我必须添加一个额外的函数调用并在经文之间打印一条线,如果我在功能调用之间打印一条空线,但是如果我添加一个空的print直到我的happybirthday()
我遇到缩进错误,但没有附加的打印行都可以正常工作,但关于为什么的任何建议?
这是代码:
def happyBirthday(person):
print("Happy Birthday to you!")
print("Happy Birthday to you!")
print("Happy Birthday, dear " + person + ".")
print("Happy Birthday to you!")
print("\n") #error line
happyBirthday('Emily')
happyBirthday('Andre')
happyBirthday('Maria')
I am following a beginners tutorial on Python, there is a small exercise where I have to add an extra function call and print a line between verses, this works fine if I print an empty line in between function calls but if I add an empty print line to the end of my happyBirthday()
I get an indent error, without the added print line all works fine though, any suggestions as to why?
Here is the code:
def happyBirthday(person):
print("Happy Birthday to you!")
print("Happy Birthday to you!")
print("Happy Birthday, dear " + person + ".")
print("Happy Birthday to you!")
print("\n") #error line
happyBirthday('Emily')
happyBirthday('Andre')
happyBirthday('Maria')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以做
一个空线。
You can just do
to get an empty line.
Python 2.x:
打印newline
python 3.x:
您必须调用函数
来源: https://docs.pypython.org/3.0/whatsnew/ 3.0.html
Python 2.x:
Prints a newline
Python 3.x:
You must call the function
Source: https://docs.python.org/3.0/whatsnew/3.0.html
如果实际上存在缩进错误,您总是只会遇到缩进错误。仔细检查您的最后一行是否与其他行相同 - 带有空格或标签。最有可能的是某些行有空间(或选项卡),而另一行有标签(或空格)。
信任错误消息 - 如果它说明了特定的内容,请假定它是真实的,并弄清楚原因。
You will always only get an indent error if there is actually an indent error. Double check that your final line is indented the same was as the other lines -- either with spaces or with tabs. Most likely, some of the lines had spaces (or tabs) and the other line had tabs (or spaces).
Trust in the error message -- if it says something specific, assume it to be true and figure out why.
不要
在最后一行。它将为您提供2条空线路。
Don't do
on the last line. It will give you 2 empty lines.
python的
print
函数在其输入中添加一个newline字符。如果不给它输入,它将仅打印一个新线字符,将打印一条空线。如果您想在打印一些文本后拥有一条额外的行,则可以在您的文本上进行新的线条,
如果您执行此操作,也可以说
print
添加2个newlines而不是仅通过更改end =
参数( end =“ \ n” ),但是您可能不需要最后一个方法,以前的两个方法更清晰。
Python's
print
function adds a newline character to its input. If you give it no input it will just print a newline characterWill print an empty line. If you want to have an extra line after some text you're printing, you can a newline to your text
If you're doing this a lot, you can also tell
print
to add 2 newlines instead of just one by changing theend=
parameter (by defaultend="\n"
)But you probably don't need this last method, the two before are much clearer.
这是在Python中打印空线的其他方法
This is are other ways of printing empty lines in python
两者在python-
上打印空白行的常见旧学校方式:
打印“ hello \ n”
单独写单词print单词会这样做:
打印“ Hello”
print
The two common to print a blank line in Python-
The old school way:
print "hello\n"
Writing the word print alone would do that:
print "hello"
print