Python中的星号金字塔程序

问题描述 投票:5回答:12

我用C ++编写了一个显示星号金字塔的程序(见下文),现在我想看看它是如何在Python中完成的,但它并不像我想象的那么容易。

有没有人试过这个,如果是这样你能告诉我代码会有帮助吗?

提前致谢。

       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************
python terminal ascii-art
12个回答
22
投票
def pyramid(rows=8):
    for i in range(rows):
        print ' '*(rows-i-1) + '*'*(2*i+1)

pyramid(8)
       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************

pyramid(12)
           *
          ***
         *****
        *******
       *********
      ***********
     *************
    ***************
   *****************
  *******************
 *********************
***********************

-1
投票

虽然我对python非常陌生,所以这就是我解决它的方法:

k=int(input("Enter the number of rows"))
for i in range(1,k):
    print(' '*(k-i),'* '*(i))

      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* * * * * * * 



-1
投票

你也可以画一个钻石

def pyramid(r):
    for i in range(r):
        print ("  "*(r-i-1) + "*"*(2*i+1))
    for i in range(r-1,-1,-1):
        print ('  '*(r-i-1) + "*"*(2*i+1))

n=int(input("Enter no of rows:"))
pyramid(n)

pyramid(10)

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *  
  * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
      * * * * * * * * * * * * *
        * * * * * * * * * * *
          * * * * * * * * *
            * * * * * * *
              * * * * *
                * * *
                  *
>>> 

-2
投票
#!/usr/bin/python
for i in range(1,6):
 for j in range(1,i+1):
   print "*",
 print

O/P: 
===
*
* *
* * *
* * * *
* * * * *

2) 
#!/usr/bin/python
for i in range(1,6):
 for j in range(1,7-i):
   print "*",
 print

O/P:
* * * * *
* * * *
* * *
* *
*

3)
#!/usr/bin/python
for i in range(1,6):
 for j in range(1,6-i):
   print "",
 for k in range(1,i+1):
  print "*",
 print

O/P:

    *
   * *
  * * *
 * * * *
* * * * *

4)
#!/usr/bin/python
for i in range(1,6):
 for j in range(1,1+i):
   print "",
 for k in range(1,7-i):
  print "*",
 print

O/P:
 * * * * *
  * * * *
   * * *
    * *
     *
5) 
#!/usr/bin/python
for i in range(1,6):
 for j in range(1,6-i):
   print "",
 for k in range(1,i+1):
  print "*",
 print
for i in range(1,5):
 for j in range(1,1+i):
   print "",
 for k in range(1,6-i):
  print "*",
 print


O/P:
    *
   * *
  * * *
 * * * *
* * * * *
 * * * *
  * * *
   * *
    *

5
投票

或者你可以尝试:

def pyramid(size=8):
    for i in range(size):
        row = '*'*(2*i+1)
        print row.center(2*size)

3
投票

您可以像这样使用字符串乘法:

>>> for i in range(size):
...     print '%s%s'%(' '*(size-(i-1)),'*'*((i*2)-1))
...

2
投票

这段代码不是很pythonic,但它是可读的。看看Hugh Bothewell对紧凑金字塔绘图功能的回答:

def drawPyramid(rows):
  result = ''

  for i in xrange(rows):
    row = ''
    row += ' ' * (rows - i - 1)
    row += '*' * (2 * i + 1)

    result += row + '\n'

  return result

print drawPyramid(20)

2
投票

我建议以下功能:

def pyramid(rows=8):
    pyramid_width = rows * 2
    for asterisks in range(1, pyramid_width, 2):
        print("{0:^{1}}".format("*" * asterisks, pyramid_width))

然后尝试:

pyramid()

或者:

pyramid(4)

1
投票
Pyramid, Inverted Pyramid and Diamond Rhombus in Python:

Pyramid

i=1
j=5
while i<=5:
 print((j*' ')+i*'* ')
 j=j-1
 i=i+1




     * 
    * * 
   * * * 
  * * * * 
 * * * * *


Inverted Pyramid

i=1
j=5
while i<=5:
 print((i*' ')+j*'* ')
 j=j-1
 i=i+1

 * * * * * 
  * * * * 
   * * * 
    * * 
     * 

Diamond Rhombus

i=1
j=5
while i<=5:
 print((j*' ')+i*'* ')
 while j<=5 & i==5:
  print(((j+1)*' ')+(5-j)*'* ')
  j=j+1
 j=j-1
 i=i+1



     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 

1
投票

如果你喜欢列表推导:

> n = 5
> print("\n".join((i*"*").center(n*2) for i in range(1, n*2, 2)))

    *
   ***
  *****
 *******
*********

0
投票
$ cat tree.py
def line(n, i):
    line = ''
    for j in range(n - i - 1):
        line += ' '
    for j in range(2 * i + 1):
        line += '*'
    for j in range(n - i - 1):
        line += ' '
    return line

def tree(n):
    for i in range(n):
        line_ = line(n, i)
        print(line_)

def run():
    tree(3)

if __name__ == '__main__':
    run()
$ python3 tree.py 
  *  
 *** 
*****
$ _

-1
投票
def pyramid(row):
       for n in range(row):
              print(' '*(n+1)+' '*(2*(row-n))+'x'+'x'*(2*n+1))

pyramid(row=8)
© www.soinside.com 2019 - 2024. All rights reserved.