Python简单字符串

2024-03-28 21:04:23 发布

您现在位置:Python中文网/ 问答频道 /正文

它应该是什么样子:

enter word: word
enter number: 5

word
word+word
word+word+word
word+word+word+word
word+word+word+word+word

我所拥有的:

for i in range(1,number+1):
    print(word*i)

我也试过这样的方法:

for row in range(1,number+1):
    for number in range(1,row+1):
        print(word, end='+')
    print()

他们都没有按要求工作。你知道吗

感谢赛博的回答,解决方案非常简单,但是我试着用for loop和“end”以及“sep”(也许即使是loop),有没有办法只用它们来实现呢?


Tags: 方法inloopnumberforrange解决方案sep
1条回答
网友
1楼 · 发布于 2024-03-28 21:04:23
def repeat(word, num):
    for i in range(1,num+1):
        print('+'.join(word for _ in range(i)))

测试

>>> repeat('hello',4)
hello
hello+hello
hello+hello+hello
hello+hello+hello+hello

相关问题 更多 >