如何重新创建金字塔三角形? [英] How to recreate pyramid triangle?

查看:30
本文介绍了如何重新创建金字塔三角形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个递归函数asterisk_triangle,它接受一个整数,然后返回一个由那么多行组成的星号三角形.

例如,这是一个 4 行星号三角形.

<代码> **********

我试过这个功能:

def asterix_triangle(depth):行 = [ (depth-i)*' ' + i*2*'*' + '*' for i in range(depth-1) ]对于 i 在行中:打印我

还有以下功能:

def asterisk_triangle(rows=n):金字塔宽度 = n * 2对于范围内的星号 (1, pyramid_width, 2):print("{0:^{1}}".format("*" * asterisks, pyramid_width))

而且都没有奏效.我应该让 tests.py 来测试功能,但我得到了错误,例如

回溯(最近一次调用):文件C:\Users\akumaukpo\Documents\CISC 106\LAB05\lab05 _test.py",第 19 行,在 <module> 中.从 lab05 导入 *文件C:\Users\akumaukpo\Documents\CISC 106\LAB05\lab05.py",第 22 行打印我^

解决方案

块中的每个语句必须从块的开头至少缩进一个空格.代码中的打印语句相对于它所包含的 for 块没有缩进,这就是出现错误的原因.

试试这个:

def asterix_triangle(depth):行 = [ (depth-i)*' ' + i*'*' + '*' for i in range(depth) ]对于 i 在行中:打印我

产生的结果:

<预><代码>>>>asterix_triangle(4)**********

我刚刚意识到您想要的输出是三角形的两半.如果是这样,只需通过在字符串的右侧添加相同的内容来镜像字符串:

def asterix_triangle(depth):行 = [ (depth-i)*' ' + i*'*' + '*' + i*'*' for i in range(depth) ]对于行中的 j:打印 j

输出:

<预><代码>>>>asterix_triangle(4)****************

I have to write a recursive function asterisk_triangle which takes an integer and then returns an asterisk triangle consisting of that many lines.

As an example this is a 4 line asterisk triangle.

     *
    **
   ***
  ****

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 e.g

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.

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)
    *
   ***
  *****
 *******

这篇关于如何重新创建金字塔三角形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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