Creating an upside down asterisk triangle in Python using for loop
<p>I hate that I have to ask this, but I can't for the life of me figure out how to make this work.  The program is supposed to ask for an input of an odd integer and then create an upside down pyramid with the first row containing the amount of asterisks as the number, and the last row having only one, centered asterisk.  I've managed to figure out most of it, but my asterisks refuse to line up centered no matter what I try.  I've looked at the other topics similar here, and tried to use them but still cannot figure it out. I'm not sure why 'i' is being used, but saw it on another post and it looked marginally better than what I had before.</p>

<p>Here is my code, I have tinkered with it quite a bit to no avail.  </p>

<pre><code>x=input('Enter an odd number width: ')
x_int = int(x)
print('Triangle:')
for i in range(x_int+1, 0, -1) :
    numwhite = (x_int - i)/2
    white_int= int(numwhite)
    print(' '* white_int + '*'*i)
</code></pre>

<p>Which outputs (input 13):</p>

<pre><code>Triangle:
**************
*************
************
 ***********
 **********
  *********
  ********
   *******
   ******
    *****
    ****
     ***
     **
      *
</code></pre>

<p>I want it to look something like (input 7)</p>

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

<p>You need to set the 'step' of the <code>range</code> to <code>-2</code>, so that it only grabs odds ints in reverse. Then the number of white spaces needed to have a <code>+ 1</code> to work properly.  </p>

<pre><code>x=input('Enter an odd number width: ')
x_int = int(x)
print('Triangle:')
for i in range(x_int, 0, -2):
    numwhite = ((x_int - i)/2) + 1
    white_int= int(numwhite)
    print(' ' * white_int + '*'*i)
</code></pre>

<p>Ouput:</p>

<pre><code>Enter an odd number width: 7
Triangle:
*******
 *****
  ***
   *
</code></pre>

<p><strong>More readable code with explanations</strong></p>

<pre><code>x_int = int(input('Enter an odd number width: '))  # reducing

print('Triangle:')

for i in range(x_int, 0, -2):  # -2 means only do every 2nd number in reverse
    num_white = int(((x_int - i)/2) + 1)  # Checking the math here, needed a +1
    print(' ' * num_white + '*' * i)  # Could be changed to .format but works
</code></pre>

<p>Output</p>

<pre><code>Enter an odd number width: 13
Triangle:
 *************
  ***********
   *********
    *******
     *****
      ***
       *
</code></pre>

python python-3.x for-loop