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
return
followed by a string.The correct syntax for
print
isprint(...)
, notprint ...
.Putting a space between sentences is not quite clean with a
for
loop, because you need to take care of a trailing space (or leading space).For example,
"I am super excited for this course! " * 5
would 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
join
to insert spaces between five sentences. If the instructor wants you to learnfor
loop specifically, then you can try:JavaScript181def show_excitement():
2s = ""
3for i in range(5):
4if i > 0:
5s += ' '
6s += "I am super excited for this course!"
7return s
8