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("输入错误,请重新输入")
结果如下: