python实现输入任意一个大写字母生成金字塔的示例

yipeiwu_com6年前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("输入错误,请重新输入")

结果如下:

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python中关键字nonlocal和global的声明与解析

一、Python中global与nonlocal 声明 如下代码 a = 10 def foo(): a = 100 执行foo() 结果 a 还是10 函数中对变...

python3生成随机数实例

本文实例讲述了python3生成随机数的方法。分享给大家供大家参考。具体实现方法如下: 该实例是根据一本书上看到过一个随机数的小程序,经过自己改动,变为了一个猜数字的小游戏,现在在pyt...

Django如何简单快速实现PUT、DELETE方法

使用django的小伙伴们应该都知道我们是无法开心的处理PUT跟DELETE的 $.ajax({ url: 'XXX', type: 'PUT', dataType: '...

python执行外部程序的常用方法小结

本文实例总结了python执行外部程序的常用方法。分享给大家供大家参考。具体分析如下: 在python中我们可以通过下面的方法直接调用系统命令或者外部程序,使用方便 1、os模块的exe...

pytorch:实现简单的GAN示例(MNIST数据集)

我就废话不多说了,直接上代码吧! # -*- coding: utf-8 -*- """ Created on Sat Oct 13 10:22:45 2018 @author: w...