Python 多行返回语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18555846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:04:24  来源:igfitidea点击:

Return statement on multiple lines

pythonpython-2.7

提问by Vox

Have scoured the interwebs trying to figure this out but with no luck. As far as I know you usually only have one return statement however my problem is that I need to have line breaks in my return statement in order for the testing to return 'true'. What I've tried is throwing up errors, probably just a rookie mistake. My current function with no attempts to make a line break is below.

已经搜索了互联网,试图解决这个问题,但没有运气。据我所知,您通常只有一个 return 语句,但是我的问题是我的 return 语句中需要换行,以便测试返回“true”。我试过的是抛出错误,可能只是一个新手错误。我当前没有尝试换行的功能如下。

def game(word, con):
   return (word + str('!')
   word + str(',') + word + str(phrase1)

Are new line breaks (\n) supposed to work in return statements? It's not in my testing.

换行符 (\n) 是否应该在 return 语句中工作?它不在我的测试中。

采纳答案by scott_fakename

In python, an open paren causes subsequent lines to be considered a part of the same line until a close paren.

在 python 中,打开括号会导致后续行被视为同一行的一部分,直到关闭括号。

So you can do:

所以你可以这样做:

def game(word, con):
    return (word + str('!') +
            word + str(',') +
            word + str(phrase1))

But I wouldn't recommend that in this particular case. I mention it since it's syntactically valid and you might use it elsewhere.

但在这种特殊情况下,我不建议这样做。我提到它是因为它在语法上是有效的,你可以在其他地方使用它。

Another thing you can do is use the backslash:

您可以做的另一件事是使用反斜杠:

def game(word, con):
    return word + '!' + \
           word + ',' + \
           word + str(phrase)
    # Removed the redundant str('!'), since '!' is a string literal we don't need to convert it

Or, in this particular case, my advice would be to use a formatted string.

或者,在这种特殊情况下,我的建议是使用格式化的字符串。

def game(word, con):
    return "{word}!{word},{word}{phrase1}".format(
        word=word, phrase1=phrase1")

That looks like it's functionally equivalent to what you're doing in yours but I can't really know. The latter is what I'd do in this case though.

看起来它在功能上等同于你正在做的事情,但我真的不知道。后者是我在这种情况下会做的。

If you want a line break in the STRING, then you can use "\n" as a string literal wherever you need it.

如果您想在 STRING 中换行,那么您可以在任何需要的地方使用“\n”作为字符串文字。

def break_line():
    return "line\nbreak"

回答by TerryA

You can split up a line in a return statement, but you have forgotten a parenthesis at the end and that you also need to separate it with another operator (in this case, a +)

您可以在 return 语句中拆分一行,但您忘记了末尾的括号,并且您还需要将其与另一个运算符分隔(在本例中为 a +

Change:

改变:

def game(word, con):
   return (word + str('!')
   word + str(',') + word + str(phrase1)

To:

到:

def game(word, con):
   return (word + str('!') + # <--- plus sign
   word + str(',') + word + str(phrase1))
#                                       ^ Note the extra parenthesis


Note that calling str()on '!'and ','is pointless. They are already strings.

请注意,调用str()'!'','是没有意义的。它们已经是字符串。

回答by James Polley

First - you're using str() to convert several strings to strings. This is not neccessary.

首先 - 您使用 str() 将多个字符串转换为字符串。这不是必需的。

Second - there's nothing in your code that would insert a newline in the string you're building. Just having a newline in the middle of the string doesn't add a newline, you need to do that explicitly.

其次 - 您的代码中没有任何内容可以在您正在构建的字符串中插入换行符。只是在字符串中间有一个换行符不会添加换行符,您需要明确地这样做。

I think that what you're trying to do would be something like this:

我认为你想要做的是这样的:

def game(word, con):
    return (word + '!' + '\n' +
        word + ',' + word + str(phrase1))

I'm leaving in the call to str(phrase1)since I don't know what phrase1 is - if it's already a string, or has a .__str__()methodthis shouldn't be needed.

我要离开的调用str(phrase1),因为我不知道什么是phrase1 -如果它已经是一个字符串,或有一个.__str__()方法,这不应该是必要的。

I'm assuming that the string you're trying to build spans the two lines, so I've added the missing parenthesis at the end.

我假设您尝试构建的字符串跨越两行,因此我在末尾添加了缺少的括号。