打印空线?

发布于 2025-02-08 16:13:10 字数 486 浏览 2 评论 0原文

我正在关注有关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 技术交流群。

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

发布评论

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

评论(7

指尖微凉心微凉 2025-02-15 16:13:10

您可以做

print()

一个空线。

You can just do

print()

to get an empty line.

烟沫凡尘 2025-02-15 16:13:10

Python 2.x:
打印newline

print      

python 3.x:
您必须调用函数

print() 

来源: https://docs.pypython.org/3.0/whatsnew/ 3.0.html

Python 2.x:
Prints a newline

print      

Python 3.x:
You must call the function

print() 

Source: https://docs.python.org/3.0/whatsnew/3.0.html

那支青花 2025-02-15 16:13:10

如果实际上存在缩进错误,您总是只会遇到缩进错误。仔细检查您的最后一行是否与其他行相同 - 带有空格或标签。最有可能的是某些行有空间(或选项卡),而另一行有标签(或空格)。

信任错误消息 - 如果它说明了特定的内容,请假定它是真实的,并弄清楚原因。

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.

陈年往事 2025-02-15 16:13:10

不要

print("\n")

在最后一行。它将为您提供2条空线路。

Don't do

print("\n")

on the last line. It will give you 2 empty lines.

私野 2025-02-15 16:13:10

python的print函数在其输入中添加一个newline字符。如果不给它输入,它将仅打印一个新线字符,

print()

将打印一条空线。如果您想在打印一些文本后拥有一条额外的行,则可以在您的文本上进行新的线条,

my_str = "hello world"
print(my_str + "\n")

如果您执行此操作,也可以说print添加2个newlines而不是仅通过更改end =参数( end =“ \ n” ),

print("hello world", end="\n\n")

但是您可能不需要最后一个方法,以前的两个方法更清晰。

Python's print function adds a newline character to its input. If you give it no input it will just print a newline character

print()

Will 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

my_str = "hello world"
print(my_str + "\n")

If you're doing this a lot, you can also tell print to add 2 newlines instead of just one by changing the end= parameter (by default end="\n")

print("hello world", end="\n\n")

But you probably don't need this last method, the two before are much clearer.

阳光下的泡沫是彩色的 2025-02-15 16:13:10

这是在Python中打印空线的其他方法

# using \n after the string creates an empty line after this string is passed to the the terminal.
print("We need to put about", average_passengers_per_car, "in each car. \n") 
print("\n") #prints 2 empty lines 
print() #prints 1 empty line 

This is are other ways of printing empty lines in python

# using \n after the string creates an empty line after this string is passed to the the terminal.
print("We need to put about", average_passengers_per_car, "in each car. \n") 
print("\n") #prints 2 empty lines 
print() #prints 1 empty line 
破晓 2025-02-15 16:13:10

两者在python-

  1. 上打印空白行的常见旧学校方式:

    打印“ hello \ n”

  2. 单独写单词print单词会这样做:

    打印“ Hello”

    print

The two common to print a blank line in Python-

  1. The old school way:

    print "hello\n"

  2. Writing the word print alone would do that:

    print "hello"

    print

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