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.
Return the string with “return”.
You can only have the string once in your code.
Don’t just copy/paste it 5 times into a single variable!
def show_excitement():
# Your code goes here!
for i in range(1, 6):
print ("I am super excited for this course! ")
return " "
print show_excitement()
How to solve this please help
Advertisement
Answer
Try the following:
def show_excitement():
return ' '.join(["I am super excited for this course!"] * 5)
print(show_excitement())
# 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!
You were almost there.
You printed the sentence five times inside your function, but the instruction requires that you return a string (without printing inside the function). So use
returnfollowed by a string.The correct syntax for
printisprint(...), notprint ....Putting a space between sentences is not quite clean with a
forloop, because you need to take care of a trailing space (or leading space).For example,
"I am super excited for this course! " * 5would result in
"I am super excited for this course! ... I am super excited for this course! "(Note the final blank in the string.)
Here I used
jointo insert spaces between five sentences. If the instructor wants you to learnforloop specifically, then you can try:def show_excitement(): s = "" for i in range(5): if i > 0: s += ' ' s += "I am super excited for this course!" return s