Python Recursion vs Iteration
<p>I can't find the error in my code. It runs just fine on my cmd but won't pass the lab platform tests. Here is the question:</p>

<blockquote>
  <p>You need to design an iterative and a recursive function called replicate_iter and replicate_recur respectively which will receive two arguments: times which is the number of times to repeat and data which is the number or string to be repeated.</p>
</blockquote>

<p>The function should return an array containing repetitions of the data argument. For instance, <code>replicate_recur(3, 5)</code> or <code>replicate_iter(3,5)</code> should return <code>[5,5,5]</code>. If the times argument is negative or zero, return an empty array. If the argument is invalid, raise a <code>ValueError</code>."</p>

<p>Now here is my code:</p>

<pre><code>my_list1 = []
my_list2 = []
   def replicate_iter(times,data):
      try:
        if type(data) != int and type(data) != str:
          raise ValueError('Invalid Argument')
        times += 0
          if times &gt; 0:
            x = 0
            while x &lt; times:
            my_list2.append(data)
            x = x+1
           return my_list2
         else:
          return []
    except (ValueError,AttributeError,TypeError):
        raise ValueError('Invalid Argument')

def replicate_recur(times,data):
    try:
        if type(data) != int and type(data) != str:
          raise ValueError('Invalid Argument')
        times += 0
        if times &lt;= 0:
          return my_list1
        else:
          my_list1.append(data)
          return replicate_recur(times-1,data)
    except(AttributeError,TypeError):
        raise ValueError('Invalid Argument')
</code></pre>

<p>This works!</p>

<pre><code>def replicate_iter(times, data):
    if((not isinstance(times, int)) or (not isinstance(data, (int, float, long, complex, str)))):
            raise ValueError("Invalid arguments")
    elif(times &lt;= 0):
            return []
    else:
            array = []
            for x in range(times):
                    array.append(data)
            return array


def replicate_recur(times, data):
    if((not isinstance(times, int)) or (not isinstance(data, (int, float, long, complex, str)))):
            raise ValueError("Invalid arguments")
    elif(times &lt;= 0):
            return []
    else:
            return ([data] + replicate_recur((times - 1), data))        
</code></pre>

python recursion iteration