<p>要使用<code>for</code>循环:</p>
<pre><code>num = int(raw_input('How high do you want the diamond to be? ')) #To get input
for i in range(1, num+1): #For loop to loop through numbers
print ' '*(num-i)+'* '*i #Print the indent first, and then the stars for the diamond
for i in range(1, num+1): #Second for loop for other half of diamond
print ' '*(i)+'* '*(num-i) #Print the indent first, and then the stars for the diamond
</code></pre>
<p>其运行方式如下:</p>
<pre><code>bash-3.2$ python diamond.py
How high do you want the diamond to be? 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
bash-3.2$ python diamond.py
How high do you want the diamond to be? 21
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
bash-3.2$ python diamond.py
How high do you want the diamond to be? 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
bash-3.2$
</code></pre>