Weird for loop in python 2.7
<p>I have a codewars problem and i'm new to python (less than 24hrs using it).
I'm solving the diamond problem:</p>

<blockquote>
  <p>Task:</p>
  
  <p>You need to a string that when printed, displays a diamond shape on
  the screen using asterisk ("*") characters. Please see provided test
  cases for exact output format.</p>
  
  <p>The shape that will be returned from print method resembles a diamond,
  where the number provided as input represents the number of *âs
  printed on the middle line. The line above and below will be centered
  and will have 2 less *âs than the middle line. This reduction by 2 *âs
  for each line continues until a line with a single * is printed at the
  top and bottom of the figure.</p>
  
  <p>Return null if input is even number or negative (as it is not possible
  to print diamond with even number or negative number).</p>
  
  <p>Please see provided test case(s) for examples.</p>
  
  <p>Python Note</p>
  
  <p>Since print is a reserved word in Python, Python students must
  implement the diamond(n) method instead, and return None for invalid
  input.</p>
</blockquote>

<p>My code:</p>

<pre><code>def diamond(n):
    retorno = " *\n"
    if n%3 == 0:
        for i in range(n,0,-2):
            retorno += i * "*" 
            print(retorno + str(i));
    #return retorno
</code></pre>

<p>Test case:</p>

<pre><code>expected =  " *\n"
expected += "***\n"
expected += " *\n"
test.assert_equals(diamond(3), expected)
</code></pre>

<p>The output:</p>

<pre><code> *
***3
 *
****1
</code></pre>

<p>How come the first "*" from the initialization of the var is repeating like it's inside the for loop?</p>

<p>It isn't repeating. It starts life as *\n, the first time through the loop you add '***' so it becomes *\n***, the next time, you add '*' so it becomes *\n****, thus the output:</p>

<pre><code>*
***

*
****
</code></pre>

<p>Note also that n%3 is not the way to test for odd numbers, you want n%2==1.</p>

python python-2.7 for-loop