【问题标题】:Print rectangle in python terminal在python终端中打印矩形
【发布时间】:2019-11-25 20:33:03
【问题描述】:

我正在尝试编写一个函数,在终端中打印出矩形。我的函数接受单个输入参数 N 并输出带有 ASCII 艺术的字符串。输出应该是这样的:

N = 2                           N = 6
########                        ####################
#      #                        #                  #
#  **  #                        #                  #
#  **  #                        #                  #
#      #                        #      ******      #
########                        #      ******      #
                                #      ******      #
N = 4                           #      ******      #
##############                  #      ******      #
#            #                  #      ******      #
#            #                  #                  #
#    ****    #                  #                  #
#    ****    #                  #                  #
#    ****    #                  ####################
#    ****    #
#            #
#            #
##############

这是我目前所拥有的:

def flag(N):
    if N % 2 == 0:
        border_j = (N * 3) + 2
        border_i = (N * 2) + 2
        for i in range(border_i):
            for j in range(border_j):
                if i in [0, border_i - 1] or j in [0, border_j - 1]:
                    print('#', end='')
                elif j == N + 1 and i == 1 + N / 2:
                    print('*', end='')
                else:
                    print(' ', end='')
            print()
    else:
        raise AssertionError


flag(2)

输出:

########
#      #
#  *   #
#      #
#      #
########

在这之后我有点困惑。接下来我该怎么做?

【问题讨论】:

  • 接下来你应该去检查条件elif j == N + 1 and i == 1 + N / 2:是否有问题

标签: python ascii


【解决方案1】:

晚了一年,但也许这对其他人有用。

代码:

def rectangle(n) -> None:
    x = n*3 + 2
    y = n*2 + 2
    [
        print(''.join(i))
        for i in
        (
            '#'*x
            if i in (0,y-1)
            else
            (
                f'#{" "*n}{"*"*n}{" "*n}#'
                if i >= (n+2)/2 and i <= (3*n)/2
                else
                f'#{" "*(x-2)}#'
            )
            for i in range(y)
        )
    ]

rectangle(0)
rectangle(1)
rectangle(2)
rectangle(4)
rectangle(6)

输出:

##
##
#####
#   #
#   #
#####
########
#      #
#  **  #
#  **  #
#      #
########
##############
#            #
#            #
#    ****    #
#    ****    #
#    ****    #
#    ****    #
#            #
#            #
##############
####################
#                  #
#                  #
#                  #
#      ******      #
#      ******      #
#      ******      #
#      ******      #
#      ******      #
#      ******      #
#                  #
#                  #
#                  #
####################

【讨论】:

    【解决方案2】:

    您可以使用更少的代码行来完成此操作,只需将其分解为您要打印的行类型,并以正确的顺序打印它们。我建议使用以下代码,它适用于任何正偶数,并且打印输出将与您正在使用的窗口大小保持一致:)

     def flag(N):
        if ((N%2 != 0) or (N <= 0)) :
            return print("Error: Requires N%2 == 0 and N positive")
        pound_sign = '#'*(3*N+2)
        blank_line = "#"+(' '*(3*N))+'#'
        grid_line = "#"+(' '*N)+('*'*N)+(' '*N)+"#"
        print('{}{}{}{}{}'.format(pound_sign+'\n',\
                        (blank_line+'\n')*int(N/2),\
                        (grid_line+'\n')*N,\
                        (blank_line+'\n')*int(N/2),\
                        pound_sign))
    

    【讨论】:

    • 非常有趣的解决方案。谢谢
    • 你试过这个代码吗?它需要的运行时间比帖子中建议的代码少得多。此外, print() 是一个非常昂贵的系统调用。我认为在这种情况下并不重要。
    猜你喜欢
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 2014-05-18
    • 2014-06-18
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多