python # 5次,其中每个句子由单个空格分隔

8ehkhllq  于 2023-01-19  发布在  Python
关注(0)|答案(7)|浏览(85)

为什么是这样的错误。我不明白它写一个函数叫“show_excitation”字符串“我超级兴奋这门课!”返回正好5次,其中每个句子是由一个单一的空格分隔。返回字符串与“return”。你只能有一个字符串在你的代码一次。不要只是复制/粘贴到一个变量5次!

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

for x in range(5):
    str =  str + " "
    print(str)
tf7tbtn2

tf7tbtn21#

在字符串的末尾给予一个空格

return "I am super excited for this course! "*5
42fyovps

42fyovps2#

在这种情况下,您可能需要str.join

def show_excitement():
    return ' '.join(['I am super excited for this course!'] * 5)

示例

>>> 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!'
iovurdzv

iovurdzv3#

print(str)移动到行首

for x in range(5):
    str =  str + " "
print(str)
dgtucam1

dgtucam14#

最简单的方法

def show_excitement():
    # Your code goes here!
    string="I am super excited for this course! " *5 
    return string

print show_excitement()
xzabzqsa

xzabzqsa5#

你可以这样做:

def show_excitement():
        s=""
        # Your code goes here!
        for i in range(0,5):
            s+='I am super excited for this course!'+" "
        return s
    
    print show_excitement()
qnyhuwrf

qnyhuwrf6#

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

oxiaedzo7#

def show_excitement():
  string = "I am super excited for this course! "
  return "\"{} {} {} {}{}\".".format(string,string,string,string,string)

print(show_excitation())在此输入代码'

相关问题