python實現輸入任意一個大寫字母生成金字塔的示例


輸入任意一個大寫字母,生成金字塔圖形

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("輸入錯誤,請重新輸入")

  

結果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM