Python中的星号金字塔程序 [英] Pyramid of asterisks program in Python

查看:784
本文介绍了Python中的星号金字塔程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



有没有人尝试过,如果可以的话,您能告诉我可以帮助您的代码吗?



请先谢谢。 / p>

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


解决方案

  def金字塔(行= 8):
for i in range(行):
print''*(rows-i-1)+' *'*(2 * i + 1)

金字塔(8)
*
***
*****
*** ****
*********
***********
*************
***************

金字塔(12)
*
***
**** *
*******
*********
***********
****** *******
***************
*****************
*******************
***** ****************
***********************


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.

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

Thanks in advance.

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

解决方案

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

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

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

这篇关于Python中的星号金字塔程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆