用Python输出ABCDE棱形
时间: 2023-11-05 07:00:02 浏览: 97
以下是用 Python 输出ABCDE棱形的代码:
```python
n = 5
for i in range(n):
# 打印左侧空格
for j in range(n-i-1):
print(" ", end="")
# 打印左侧字符
for j in range(i+1):
print(chr(65+j), end="")
# 打印右侧字符
for j in range(i):
print(chr(64+i-j), end="")
# 打印右侧空格
for j in range(n-i-1):
print(" ", end="")
print() # 换行
for i in range(n-2, -1, -1):
# 打印左侧空格
for j in range(n-i-1):
print(" ", end="")
# 打印左侧字符
for j in range(i+1):
print(chr(65+j), end="")
# 打印右侧字符
for j in range(i):
print(chr(64+i-j), end="")
# 打印右侧空格
for j in range(n-i-1):
print(" ", end="")
print() # 换行
```
输出结果如下:
```
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
```
阅读全文