看到这个题目时,可以分析每一行字符的组成规律,就可以分成上下两部来实现。
实现的代码如下:
#python 3.6
import sys
print('请输入要打印的字符: ')
printChar = input()
print('请输入层数: ')
count = input()
count = int(count)
rowLen = count
#输出上半部分行
for i in range(count):
output = ' '*(rowLen - i) + printChar
for k in range(i):
output += ' ' * rowLen + printChar
print(output)
#输出下半部分行
count -= 1
for i in range(count):
output = ' '* (i+2) + printChar
for k in range(count - i - 1):
output += ' ' * rowLen + printChar
print(output)
结果如下: