Python中的星号金字塔程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4911341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 18:05:10  来源:igfitidea点击:

Pyramid of asterisks program in Python

pythonterminalascii-art

提问by jimmyc3po

I've written a program in C++ that displays a pyramid of asterisk (see below) and now I'd like to see how it's done in Python but it's not as easy as I'd thought it would be.

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

Has anyone tried this and if so could you show me code that would help out?

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

Thanks in advance.

提前致谢。

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

采纳答案by Hugh Bothwell

def pyramid(rows=8):
    for i in range(rows):
        print ' '*(rows-i-1) + '*'*(2*i+1)

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

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

回答by kelloti

You can use string multiplication like so:

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

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

回答by Blender

This code isn't very pythonic, but it's readable. Look at Hugh Bothewell's answer for a compact pyramid drawing function:

这段代码不是很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)

回答by Chaos Manor

I would suggest the following function:

我建议使用以下功能:

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

Then try with:

然后尝试:

pyramid()

or with:

或与:

pyramid(4)

回答by kleytont

Or you could try:

或者你可以试试:

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

回答by user3416044

You can also draw a DIAMOND

你也可以画一个DIAMOND

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)

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

回答by John Don

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

pyramid(row=8)

回答by Rohit

Though I am very much new to python, so here is how I solved it:

虽然我对 python 很陌生,但这是我解决它的方法:

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

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




回答by Ramesh

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



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

回答by sivaprasad

#!/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:
    *
   * *
  * * *
 * * * *
* * * * *
 * * * *
  * * *
   * *
    *