本文介绍了我如何在Python中创建一个递减的numberPyramid(num)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个如下图所示的金字塔(numberPyramid(6)),金字塔不是由数字组成的,而是实际上是一个黑色的空格。该函数接受一个名为num的参数,它是金字塔中的行数。我怎么去做这个?我需要使用for循环,但我不知道如何实现它。

  666666666666 
55555 55555
4444 4444 $ b $ 333 333
22 22
1 1


解决方案



  max_depth = int(raw_input(输入最大金字塔深度(2  -  9):))
我在范围(max_depth,0,-1):
print str(i)* i +*((max_depth-i)* 2)+ str(i)* i


输出:

 (numpyramid )macbook:numpyramid joeyoung $ python numpyramid.py 
输入金字塔的最大深度(2 - 9):6
666666666666
55555 55555
4444 4444
333 333
22 22
1 1

这是如何工作的:



Python有一个名为的内置函数 range() 它可以帮助您为 for-loop 构建迭代器。你可以通过传递 -1 作为第三个参数来减少递增,而不是递增。

我们的for循环将会从用户提供的 max_depth (我们的例子中为6)和 i 会为循环的每次迭代递减1 。

现在输出行应该执行以下操作:
$ b $ ul

  • 打印出当前的迭代器编号( i )并重复 i 次。在中间添加多少空白。


    • 这将是max_depth减去当前迭代器数量,然后将结果乘以2,因为每次迭代需要将空白加倍。 >

    • 附加第二组重复的数字:当前的迭代器数量( i )重复 i 次数


    打印字符时,可以用星号 * 跟随字符来重复这些字符,以及字符的次数重复。
    例如:

     >>> #重复字符'A'5次
    ...打印A* 5
    AAAAA


    I'm trying to create a pyramid that looks like the picture below(numberPyramid(6)), where the pyramid isn't made of numbers but actually a black space with the numbers around it. The function takes in a parameter called "num" and which is the number of rows in the pyramid. How would I go about doing this? I need to use a for loop but I'm not sure how I implement it. Thanks!

                                     666666666666
                                     55555  55555
                                     4444    4444
                                     333      333
                                     22        22
                                     1          1
    
    解决方案

    First the code:

    max_depth = int(raw_input("Enter max depth of pyramid (2 - 9): "))
    for i in range(max_depth, 0, -1):
        print str(i)*i + " "*((max_depth-i)*2) + str(i)*i
    

    Output:

    (numpyramid)macbook:numpyramid joeyoung$ python numpyramid.py
    Enter max depth of pyramid (2 - 9): 6
    666666666666
    55555  55555
    4444    4444
    333      333
    22        22
    1          1
    

    How this works:

    Python has a built-in function named range() which can help you build the iterator for your for-loop. You can make it decrement instead of increment by passing in -1 as the 3rd argument.

    Our for loop will start at the user supplied max_depth (6 for our example) and i will decrement by 1 for each iteration of the loop.

    Now the output line should do the following:

    • Print out the current iterator number (i) and repeat it itimes.
    • Figure out how much white space to add in the middle.
      • This will be the max_depth minus the current iterator number, then multiply that result by 2 because you'll need to double the whitespace for each iteration
    • Attach the whitespace to the first set of repeated numbers.
    • Attach a second set of repeated numbers: the current iterator number (i) repeated itimes

    When your print characters, they can be repeated by following the character with an asterisk * and the number of times you want the character to be repeated.For example:

    >>> # Repeats the character 'A' 5 times
    ... print "A"*5
    AAAAA
    

    这篇关于我如何在Python中创建一个递减的numberPyramid(num)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 11-02 07:39