简体   繁体   中英

how to return string n times from a python function, such that it is printed n times with space in between

I am supposed to Write a function called "show_excitement" where the string "I am super excited for this course!" is returned exactly 5 times, where each sentence is separated by a single space. I can only have the string once in my code.

I have tried following code, but I think it is wrong somewhere. Please give an improved or alternate solution.

def show_excitement(str,n):
    if(n==0):
        return str
    else:
        show_excitement(str,n-1)

str="I am super excited for this course!"
print show_excitement(str,5)

怎么样的东西:

" ".join(["hello"] * 5)

try this:

def show_excitement(str,n):
    if(n==0):
        return str
    else:
        #return the str N times
        return str*n


str="I am super excited for this course!"

print show_excitement(str,5)

I believe you are trying to print it using recursion. Try below using recursion.

def show_excitement(str,n):
    global result
    if(n==0):
        return result.strip()
    else:
        result=result+" "+str
        return show_excitement(str,n-1)


result = ""
str="I am super excited for this course!"
print show_excitement(str,5)

Output:

I am super excited for this course! I am super excited for this course! I am super excited for this course! I am super excited for this course! I am super excited for this course!

def show_excitement():
    str = "I am super excited for this course!"
    return  (str + " ")* 5

print show_excitement()

Return does not print strings!

def show_excitement(str,n):
        print(str)
        if(n!=1):
            show_excitement(str,n-1)

str="I am super excited for this course!"
show_excitement(str,5)

If you want to use recursion:

def show_excitement(str, n):
    global return_str
    return_str += str
    if n == 0:
        return return_str
    else:
        show_excitement(str, n - 1)


return_str = ""
str = "I am super excited for this course!"
show_excitement(str, 5)
print(return_str)

NOTE: The str variable name is not the perfect one. In my opinion you should avoid the recursion if it is possible. This is a simple task and you can solve it many different way without recursion.

To expand on my comments (using python 3):

Version with no return:

def show_excitement(str,n):
    if(n==0):
        print('\n') # last bit of code that runs in the function, no return
    else:
        print(str, end=' ')
        return show_excitement(str, n-1)

str="I am super excited for this course!"
show_excitement(str, 5) # returns None but prints to console

# result - note there will be space on end printed that you can't see
# I am super excited for this course! I am super excited for this course! I am super excited for this course! I am super excited for this course! I am super excited for this course!

Version building string and returning a string object:

def show_excitement(str, n, str_built=''):
    if(n==0):
        return str_built.strip() # clear last ' '
    else:
        str_built += str + ' ' # can be changed to '\n' if needed on new line
        return show_excitement(str, n-1, str_built)


str="I am super excited for this course!"
show_excitement(str, 5) # string object
# 'I am super excited for this course! I am super excited for this course! I am super excited for this course! I am super excited for this course! I am super excited for this course!'
print(show_excitement(str, 5))
# I am super excited for this course! I am super excited for this course! I am super excited for this course! I am super excited for this course! I am super excited for this course!

I recommend the following version unless you have to use recursion in which case I would use the string builder version.

def show_excitement(str, n, sep=" "):
    return sep.join([str] * n)
def show_excitement():

    return ("I am super excited for this course!" + " ")*5

print(show_excitement())

Output:

I am super excited for this course! I am super excited for this course! I am super excited for this course! I am super excited for this course! I am super excited for this course!

def show_excitement(stri,n): 
    if(n==1):
        return stri
    else:
        return show_excitement(stri,n-1)+" "+stri
print(show_excitement("I am super excited for this course!",5))
def show_excitement(str,n):
    print str*n

str=" I am super excited for this course!"

show_excitement(str,5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM