Python 倒金字塔(PY)

Python 倒金字塔(PY),python,Python,所以我有一个任务,要求我在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): #pr

所以我有一个任务,要求我在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
         *         
        ***        
       *****       
      *******      
     *********     
    ***********    
   *************   
  ***************  
 ***************** 
*******************

如果要反转棱锥体,只需反转外部循环即可。多亏了,您可以直接使用内置功能。 此外,还可以使用字符串乘法和函数稍微简化循环体

for i in reversed(range(p)):
    print(('*' * (1+2*i)).center(1+2*p))

我有Python3,如果这意味着什么的话,只要把最外层的循环
换成反向(范围(1,p+1)):
就这么简单。@tobias_k谢谢我是Pythonsems的新手,我们可以启动一个代码库来打印所有那些用作初学者练习的星号形状:,,@cfi我打赌那会是一本畅销书!但别忘了包括在内!