首页 > 解决方案 > 如何在python中显示以下内容?

问题描述

问题陈述是:

编写一个函数create_grid,接收正方形的边数和正方形边上的星数,然后将正方形网格作为字符串返回。用换行符 ( \n) 分隔每一行。

它应该显示以下内容:

assert_equal(
    create_grid(1, 1),
"""+ - +
| * |
+ - +
""")
assert_equal(
    create_grid(2, 4),
"""+ - - - - + - - - - +
| * * * * |         |
| * * * * |         |
| * * * * |         |
| * * * * |         |
+ - - - - + - - - - +
|         | * * * * |
|         | * * * * |
|         | * * * * |
|         | * * * * |
+ - - - - + - - - - +
""")
assert_equal(
    create_grid(4, 2),
"""+ - - + - - + - - + - - +
| * * |     | * * |     |
| * * |     | * * |     |
+ - - + - - + - - + - - +
|     | * * |     | * * |
|     | * * |     | * * |
+ - - + - - + - - + - - +
| * * |     | * * |     |
| * * |     | * * |     |
+ - - + - - + - - + - - +
|     | * * |     | * * |
|     | * * |     | * * |
+ - - + - - + - - + - - +
""")

我只能想出以下代码:

def create_grid(num_squares, num_stars):
    # YOUR CODE HERE
    #raise NotImplementedError()

    #'+ - +\n
    #| * |\n
    #+ - +\n'
    holder = ""
    a = '+ ' + "- "*num_stars + "+\n"
    b = '| ' + '* '*num_stars + '|\n'

    for squaresdown in range(num_squares):
        holder+=a
        for row in range(num_stars):
            holder+=b
    holder+=a
    print(holder)

create_grid(3,5)

显示网格

+ - - - - - +
| * * * * * |
| * * * * * |
| * * * * * |
| * * * * * |
| * * * * * |
+ - - - - - +
| * * * * * |
| * * * * * |
| * * * * * |
| * * * * * |
| * * * * * |
+ - - - - - +
| * * * * * |
| * * * * * |
| * * * * * |
| * * * * * |
| * * * * * |
+ - - - - - +

我如何实现一种方法:

标签: pythonloopsnestediteration

解决方案


考虑这种方法(更新):

def create_grid(num_squares, num_stars):
    holder = ""
    a = '+ ' + ("- " * num_stars + "+ " + "- " * num_stars + '+ ')*(
        num_squares // 2) + ("- " * num_stars + "+ ") * (num_squares % 2)
    b = ('| ' + '* ' * num_stars + '| ' + "  " * num_stars)*(num_squares//2) \
         + ('| ' + '* ' * num_stars) * (num_squares % 2)

    for column in range(num_squares):
        holder += a + "\n"
        for row in range(num_stars):
            holder += (b + '|' if (column % 2) == 0 else '|' + b[::-1]) + "\n"
    holder += a + "\n"
    print(holder)

因此,一些关于它如何打印的说明性示例:

create_grid(1,1)

+ - +
| * |
+ - +

create_grid(1,2)

+ - - +
| * * |
| * * |
+ - - +

create_grid(1,3)

+ - - - +
| * * * |
| * * * |
| * * * |
+ - - - +

create_grid(2,1)

+ - + - +
| * |   |
+ - + - +
|   | * |
+ - + - +

create_grid(2,2)

+ - - + - - +
| * * |     |
| * * |     |
+ - - + - - +
|     | * * |
|     | * * |
+ - - + - - +

create_grid(2,3)

+ - - - + - - - +
| * * * |       |
| * * * |       |
| * * * |       |
+ - - - + - - - +
|       | * * * |
|       | * * * |
|       | * * * |
+ - - - + - - - +

create_grid(3,1)

+ - + - + - +
| * |   | * |
+ - + - + - +
| * |   | * |
+ - + - + - +
| * |   | * |
+ - + - + - +

create_grid(3,2)

+ - - + - - + - - +
| * * |     | * * |
| * * |     | * * |
+ - - + - - + - - +
| * * |     | * * |
| * * |     | * * |
+ - - + - - + - - +
| * * |     | * * |
| * * |     | * * |
+ - - + - - + - - +

create_grid(3,3)

+ - - - + - - - + - - - +
| * * * |       | * * * |
| * * * |       | * * * |
| * * * |       | * * * |
+ - - - + - - - + - - - +
| * * * |       | * * * |
| * * * |       | * * * |
| * * * |       | * * * |
+ - - - + - - - + - - - +
| * * * |       | * * * |
| * * * |       | * * * |
| * * * |       | * * * |
+ - - - + - - - + - - - +

推荐阅读