Creating pattern using simple loops?
<p>I am trying to create this pattern in python:</p>

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

<p>This is my program so far that I've come up with:</p>

<pre><code>ster = "*"
space = " "
lines = 0
n = 3
x = 1
while lines &lt;= 5:
    print space*n, ster*x
    n-= 1
    x+= 1
    lines += 1
</code></pre>

<p>What am I doing wrong?</p>

<p>Okay, first of all you can create a list of numbers which represents the number of stars in each line.</p>

<pre><code>number_of_stars = 5

i_list = list(range(number_of_stars))
# extend the list by its inverse i_list[::-1]
# but exclude the first item
i_list.extend(i_list[::-1][1:])

print(i_list) # prints: [0, 1, 2, 3, 4, 3, 2, 1, 0]
</code></pre>

<p>Now you can go thru the list and print a multiple of <code>*</code></p>

<pre><code>for i in i_list:
    print('* ' * i)
</code></pre>

<p>But this is not aligned properly. By adding multiple empty spaces to the left one can archive a solution:</p>

<pre><code>for i in i_list:
    print(' ' * (number_of_stars - i) + '* ' * i)
</code></pre>

<p>Note that in python one can repeat a string by using the multiplication symbol:</p>

<pre><code>print('a'*5) # prints: aaaaa
</code></pre>

python python-2.7 loops