Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return statement on multiple lines

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.

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.

like image 975
Vox Avatar asked Sep 01 '13 06:09

Vox


People also ask

How do I return a string with multiple lines?

Use """ (double quote) or ''' (single quote) for multiline string or use \ for seperating them. NOTE: """ and ''' should be used with care otherwise it may print too many spaces in between two lines.

How do you return multiple lines in Python?

Most likely, you want a line break in the string you're returning, not in the return statement itself. That can be done with '\n' .

How do you break a long statement across multiple lines?

Instead, use the backslash ( \ ) to indicate that a statement is continued on the next line. In the revised version of the script, a blank space and an underscore indicate that the statement that was started on line 1 is continued on line 2.

Can a function have multiple return statement?

A function can have more than one return statement, but only ever run one based on a condition.


2 Answers

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

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.

def break_line():
    return "line\nbreak"
like image 157
scott_fakename Avatar answered Sep 20 '22 03:09

scott_fakename


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 +)

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.

like image 42
TerryA Avatar answered Sep 18 '22 03:09

TerryA