Multi-line return statement

They washed the intersectors, trying to figure it out, but no luck. As far as I know, you usually only have one return statement, but my problem is that I need to have line breaks in my return statement so that testing returns "true". What I tried causes errors, maybe just a rookie error. My current function without trying to make a line break below.

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

Are new line breaks (\ n) supposed to work in return statements? This is not in my testing.

+4
source share
3 answers

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

So you can do:

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

But I would not recommend this in this particular case. I mention this because it is syntactically correct and you can use it elsewhere.

Another thing you can do is use a 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") 

It seems like this is functionally equivalent to what you do in your own, but I don't know. The last is what I would do in this case.

If you want to split the string in STRING, you can use "\ n" as a string literal where you need it.

 def break_line(): return "line\nbreak" 
+9
source

You can split the line in the return statement, but in the end you forgot the brackets and that you also need to separate it from another statement (in this case, + )

Edit:

 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 ',' meaningless. They are already lines.

+2
source

First you use str () to convert multiple lines to lines. It's not obligatory.

Secondly, there is nothing in your code to insert a line of a line in the line you are building. Just having a new line in the middle of the line does not add a new line, you need to do it explicitly.

I think what you are trying to do would be something like this:

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

I am leaving the call to str(phrase1) , since I do not know what phrase1 is - if it is already a string or has a .__str__() method , this is not necessary.

I assume that the line you are trying to build spans two lines, so I added the missing bracket at the end.

+1
source

Source: https://habr.com/ru/post/1499992/


All Articles