简体   繁体   English

如何重新创建金字塔三角形?

[英]How to recreate pyramid triangle?

I have to write a recursive function asterisk_triangle which takes an integer and then returns an asterisk triangle consisting of that many lines. 我必须编写一个递归函数asterisk_triangle ,该函数需要一个整数,然后返回一个由这么多行组成的星号三角形。

As an example this is a 4 line asterisk triangle. 例如,这是一个4行星号三角形。

     *
    **
   ***
  ****

I have tried this function: 我已经尝试过此功能:

def asterix_triangle(depth):
            rows = [ (depth-i)*' ' + i*2*'*' + '*'   for i in range(depth-1) ]
            for i in rows:
            print i

And the following function: 和以下功能:

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

And neither worked. 两者都不起作用。 I am supposed to make tests.py to test the functions and I get errors for eg 我应该做tests.py来测试功能,我得到例如错误

Traceback (most recent call last):
  File "C:\Users\akumaukpo\Documents\CISC 106\LAB05\lab05 _test.py", line 19, in <module>
    from lab05 import *
  File "C:\Users\akumaukpo\Documents\CISC 106\LAB05\lab05.py", line 22
    print i
        ^

Every statement in a block must be indented by at least one space from the start of the block. 块中的每个语句必须从块开始至少缩进一个空格。 The print statement in your code is not indented relative to the for block it is contained in, which is why you have the error. 代码中的print语句相对于包含在其中的for块没有缩进,这就是为什么会出现错误。

Try this: 尝试这个:

def asterix_triangle(depth):
        rows = [ (depth-i)*' ' + i*'*' + '*'   for i in range(depth) ]
        for i in rows:
            print i

Which yields: 产生:

>>> asterix_triangle(4)
    *
   **
  ***
 ****

EDIT: 编辑:

I just realised your desired output is to have both halves of a triangle. 我刚刚意识到您想要的输出是同时拥有两个三角形。 If so, just mirror the string by adding the same thing to the right side of the string: 如果是这样,只需在字符串的右侧添加相同的内容来镜像字符串:

def asterix_triangle(depth):
        rows = [ (depth-i)*' ' + i*'*' + '*' + i*'*'  for i in range(depth) ]
        for j in rows:
            print j

The output: 输出:

>>> asterix_triangle(4)
    *
   ***
  *****
 *******

If you need to do a pyramid recursively you probably need to do something like this. 如果您需要递归做金字塔,则可能需要做这样的事情。

def asterix_triangle(i, t=0):
    if i == 0:
        return 0
    else:
        print ' ' * ( i + 1 ) + '*' * ( t * 2 + 1)
        return asterix_triangle(i-1, t + 1)

asterix_triangle(5)

The idea is that you use two variables. 这个想法是您使用两个变量。 One which is i that that subtract once every time the function is called and is used to end the cycle. i是,每次调用该函数都会减去一次,并用于结束循环。 The other variable is used to have an incremental increase. 另一个变量用于增加增量。 You then use i to print the number of spaces, and t the number of stars. 然后,使用i打印空格数,然后使用t星星数。

The output would be: 输出为:

      *
     ***
    *****
   *******
  *********
for i in range(10):
print((' '*(10-i-1))+(('*')*((2*i)-1)))

The output is as shown in the link 输出如链接中所示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM