【发布时间】:2015-10-16 20:57:38
【问题描述】:
所以我有一个任务要求我打印一个由 Python 中的星号制成的倒置金字塔。我知道如何打印出一个普通的金字塔,但我该如何翻转它? 金字塔的高度由用户的输入决定。这就是我对普通金字塔的看法:
#prompting user for input
p = int(input("Enter the height of the pyramid: "))
#starting multiple loops
for i in range(1,p+1):
for j in range(p-i):
#prints the spacing
print(" ",end='')
#does the spacing on the left side
for j in range(1,i):
print("*",end='')
for y in range(i,0,-1):
print("*",end='')
#does the spacing on the right side
for x in range(p-i):
print(" ",end='')
#prints each line of stars
print("")
输出:
Enter the height of the pyramid: 10
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
【问题讨论】:
标签: python