如何从python函数返回n次字符串,以便在n次之间以空格打印n次

时间:2019-05-15 08:02:39

标签: python string

我应该写一个名为"show_excitement"的函数,其中的字符串 准确返回"I am super excited for this course!" 5次,每个句子之间用一个空格隔开。 我的代码中只能有一个字符串。

我尝试了以下代码,但是我认为在某处是错误的。请提供改进或替代的解决方案。

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)

10 个答案:

答案 0 :(得分:1)

那又怎么样呢?

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

答案 1 :(得分:1)

尝试一下:

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)

答案 2 :(得分:1)

我相信您正在尝试使用递归打印它。请尝试以下使用递归。

public class CustomizedUserFeedRepositoryImpl<T> implements CustomizedUserFeedRepository<T> {


private CassandraOperations cassandraOperations;

@Autowired
CustomizedUserFeedRepositoryImpl(CassandraOperations cassandraOperations){
    this.cassandraOperations = cassandraOperations;

}

@Override
public <S extends T> S save(S entity, int ttl){
    InsertOptions insertOptions;
    if(ttl == 0) {
        insertOptions =  InsertOptions.builder().ttl(Duration.ofHours(24)).build();
    } else {
        insertOptions = InsertOptions.builder().ttl(ttl).build();
    }
    cassandraOperations.insert(entity,insertOptions);
    return entity;
}

@Override
public void saveAllWithTtl(java.lang.Iterable<T> entities, int ttl){
    entities.forEach(entity->{
        save(entity,ttl);
    });
}

}

输出:

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)

答案 3 :(得分:1)

ITester t1 = new FizzBazz();
t1.execute();
ITester t2 = new ReverseText();
t2.execute();

答案 4 :(得分:0)

返回打印字符串!

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)

答案 5 :(得分:0)

如果要使用递归:

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)

注意:str变量名称不是完美的名称。我认为您应尽可能避免递归。这是一个简单的任务,您无需递归即可解决许多不同的问题。

答案 6 :(得分:0)

要扩展我的评论(使用python 3):

没有退货的版本:

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!

版本构建字符串并返回一个字符串对象:

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!

除非您必须使用递归,否则我建议使用以下版本,在这种情况下,我将使用字符串生成器版本。

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

答案 7 :(得分:0)

def show_excitement():

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

print(show_excitement())

输出:

  

我对这门课程感到非常兴奋!我对这门课程感到非常兴奋!我对这门课程感到非常兴奋!我对这门课程感到非常兴奋!我对这门课程感到非常兴奋!

答案 8 :(得分:0)

<div id="reviewStars-input">
  <%= collection_radio_buttons(:comment, :rating, Comment::STARS, :first, :second) do |b| %>
    <%= b.radio_button %>
    <%= b.label( title: b.text ) %>
  <% end %>
</div>

答案 9 :(得分:-1)

def show_excitement(str,n):
    print str*n

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

show_excitement(str,5)