官方解答如下:
from sys import stdout
for i in range(4):
for j in range(2 - i + 1):
stdout.write(' ')
for k in range(2 * i + 1):
stdout.write('*')
print('')
for i in range(3):
for j in range(i + 1):
stdout.write(' ')
for k in range(4 - 2 * i + 1):
stdout.write('*')
print('')
自己解答如下:(笨方法)
line_all = int(raw_input('请输入行数:\n'))
if line_all % 2 == 0:
print('请重新输入一个奇数!')
else:
line_mid = line_all/2+1
#line_all = 9
#line_mid = line_all/2+1
for i in range(1,line_all+1):
if i < line_mid:
print ' '*((line_all-2*i+1)/2),'*'*(2*i-1),' '*((line_all-2*i+1)/2)
elif i == line_mid:
print '','*'*line_all
else:
print ' '*((2*i-line_all-1)/2),'*'*(2*line_all-2*i+1),' '*((2*i-line_all-1)/2)