Return statement on multiple lines
Return statement on multiple lines
--
Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn
--
Track title: CC F Haydns String Quartet No 53 in D
--
Chapters
00:00 Question
00:42 Accepted answer (Score 30)
01:58 Answer 2 (Score 4)
02:38 Answer 3 (Score 1)
03:12 Answer 4 (Score 1)
04:12 Thank you
--
Full question
https://stackoverflow.com/questions/1855...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python27
#avk47
ACCEPTED ANSWER
Score 32
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"
ANSWER 2
Score 4
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.
ANSWER 3
Score 1
First - you're using str() to convert several strings to strings. This is not neccessary.
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 this shouldn't be needed..__str__() method
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.
ANSWER 4
Score 1
the simple code to understand
def sentence():
return print('ankit \nkalauni is\n the\n new\n learner\n in\n programming')
sentence()
the output:
ankit
kalauni
is
the
new
learner
in
programming
easy peasy way to return the multiple lines in python