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使用BeautifulSoup库解析HTML基本使用教程

 BeautifulSoup是Python的一个第三方库,可用于帮助解析html/XML等内容,以抓取特定的网页信息。目前最新的是v4版本,这里主要总结一下我使用的v3版本解析...

Python+selenium 获取浏览器窗口坐标、句柄的方法

1.0 获取浏览器窗口坐标 python目录可找到Webdriver.py 文件定义了get_window_rect()函数,可获取窗口的坐标和大小(长宽),但出现”Command n...

python 实现分页显示从es中获取的数据方法

注意:使用该方法,获取的数据总数目不能超过1万,否则出错 #在python3上运行 from elasticsearch import Elasticsearch from urll...

tensorflow: variable的值与variable.read_value()的值区别详解

tensorflow: variable的值与variable.read_value()的值区别详解

问题 查看 tensorflow api manual 时,看到关于 variable.read_value() 的注解如图: 那么在 tensorflow 中,variable的值...

python字符串分割及字符串的一些常规方法

字符串分割,将一个字符串分裂成多个字符串组成的列表,可以理解为字符串转列表,经常会用到 语法:str.split(sep, [,max]),sep可以指定切割的符号,max可以指定切割...