python输出字母金字塔_Python中的字母金字塔程序

可能有不同的方法来有效地实现这一点,但这里有一个简单、易于理解的解决方案(基本循环)letter = raw_input('Enter Input: ')

if (len(letter) == 1):

if (letter.isalpha()):

# Retrieve the positon of the input letter

pos = ord(letter.upper()) - 64

# Prints upper part of the diamond

for i in range(1, pos + 1):

# Prints leading spaces for upper pyramid

for j in range(pos - i, 0, -1):

print(" "),

# Print numbers

# 2 for loops to print ascending and descending letters in a single row

for j in range(0, i):

print(chr(65+j)),

for j in range(i-1 , 0, -1):

print(chr(64+j)),

print

# Prints lower part of the diamond, This is just the reverse of the upper one

for i in range(pos -1 , 0, -1):

# Print leading space for lower pyramid

for j in range(pos - i, 0, -1):

print(" "),

for j in range(0, i):

print(chr(65+j)),

for j in range(i-1 , 0, -1):

print(chr(64+j)),

print

else:

print 'input a letter from the alphabet only'

else:

print 'enter one letter only'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值