Repeat a string n times and print n lines of it
<p>i've been stuck on a question for some time now:</p>

<p>I'm looking to create a python function that consumes a string and a positive integer. The function will print the string n times, for n lines. I <strong>cannot</strong> use loops, i must only use recursion</p>

<p>e.g.</p>

<pre><code>repeat("hello", 3)

hellohellohello
hellohellohello
hellohellohello
</code></pre>

<p>whenever i try to make a function that does this, the function decreases the length of the string, progressively:</p>

<p>e.g.</p>

<pre><code>repeat("hello", 3)

hellohellohello
hellohello
hello
</code></pre>

<p>here's what my code looks like:</p>

<pre><code>def repeat(a, n):
if n == 0:
    print(a*n)
else:
    print(a*n)
    repeat(a, n-1)
</code></pre>

<p>any help would be appreciated, thanks!</p>

<p>So you just need extra argument that will tell you how many times you already ran that function, and it should have default value, because in first place function must take two arguments(<code>str</code> and positive number).</p>

<pre><code>def repeat(a, n, already_ran=0):
    if n == 0:
        print(a*(n+already_ran))
    else:
        print(a*(n+already_ran))
        repeat(a, n-1, already_ran+1)
repeat('help', 3)
</code></pre>

<p>Output</p>

<pre><code>helphelphelp
helphelphelp
helphelphelp
helphelphelp
</code></pre>

python recursion