輸入任意一個大寫字母,生成金字塔圖形
def GoldTa(input):
L = [chr(i) for i in range(65, 91)] # 大寫字母A--Z
idA = 65 # 從A開始
# ord()函數將字母轉換為Unicode數值
idInput = ord(input)
num = idInput - idA + 1 # 輸入的字符個數
tempResult = ""
for C in range(0, num):
for C1 in range(0, C): # 左 [ABC]
tempResult = tempResult + L[C1]
tempResult = tempResult + L[C] # 中 [D]
for C2 in range(C - 1, -1, -1): # 右 [CBA]
tempResult = tempResult + L[C2]
for C3 in range(num - 1 - C): # 每行空格
tempResult = " " + tempResult
print(tempResult) # 輸出
tempResult = "" # 清空臨時結果
while True:
char = input("請輸入一個大寫字母:")
if char.isupper():
GoldTa(char)
continue
else:
print("輸入錯誤,請重新輸入")
結果如下:

