可能有不同的方法来有效地实现这一点,但这里有一个简单、易于理解的解决方案(基本循环)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)),
# 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)),
else:
print 'input a letter from the alphabet only'
else:
print 'enter one letter only'