简体   繁体   中英

Print rectangle in python terminal

I am trying to write a function,that prints out rectangle in a terminal. My function accepts a single input parameter N and output a string with an ASCII art. This what the output should look like:

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

This is what i have so far :

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)

Output:

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

And after this i've got a little confused. What should i do next?

You need to change the elif to this:

elif N + 1 <= j < border_j - 1 - N and 1 + N / 2 <= i < border_i - N:

Basically you need to print the * in a range and not just when it is == (exactly equal) .

Hope this helps. Good luck.

you can do this with quite a bit fewer lines of code by breaking it down into which types of lines you want to print, and printing them in the right order. I propose the following code, it will work for any positive even number and the printout will be good up to the size of the window you're using :)

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

A year late, but maybe this will be useful to someone else.

Code:

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)

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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