Drawing a hollow asterisk square

前端 未结 10 1586
情话喂你
情话喂你 2020-12-09 14:09

I\'m trying to figure out how to turn my whole square into a hollow one. The few things I\'ve tried so far haven\'t been very successful as I end up getting present

相关标签:
10条回答
  • 2020-12-09 14:30

    Nice python library that will do this...

    termshape - https://github.com/zvibazak/termshape

    from termshape import get_rectangle 
    print(get_rectangle(10, 5))
    
    * * * * * * * * * *
    *                 *
    *                 *
    *                 *
    * * * * * * * * * *
    
    0 讨论(0)
  • 2020-12-09 14:32
    size = int(input('Enter the size of square you want to print = '))
    for i in range(size):           # This defines the rows
        for j in range(size):       # This defines the columns
            print('*' , end=' ')    # Printing * and " end=' ' " is giving space      after every * preventing from changing line
        print()                     # Giving a command to change row after every column in a row is done
    
    print()                         # Leaving one line
    for k in range(size):           # This defines the rows
        print('* ' *size)           # Defines how many times * should be multiplied
    
    0 讨论(0)
  • 2020-12-09 14:34

    Try this:

    size = 5
    for i in range(1,size+1):
        if (i==1 or i==size):
            print("*"*size)
        else:
            print("*"+" "*(size-2),end="")
            print("*")
    

    output:

    *****
    *   *
    *   *
    *   *
    *****
    
    0 讨论(0)
  • 2020-12-09 14:36

    You can print out a single '*', followed by size-2 spaces, then a single '*'. This will give you the "hollow" portion. The first and last lines need the full length:

    size = 5
    inner_size = size - 2
    print ('*' * size)
    for i in range(inner_size):
        print ('*' + ' ' * inner_size + '*')
    print ('*' * size)
    
    0 讨论(0)
  • 2020-12-09 14:43

    Here is my python 3.6 code for drawing a square using column size and row size inputs:

    column = int(input("Enter column size : "))
    row = int(input("Enter row size : "))
    print('* ' * column)
    print(('* ' + "  " * (column-2)+ '*'+'\n')*(row -2) + ('* ' * column))
    
    0 讨论(0)
  • 2020-12-09 14:44

    You can also draw a triangle using this simple way:

    n = int(input("enter num")) for i in range (n+1):

    if 2>=i :
        print (i * "*")
    elif n == i:
        print ("*" * i)
    else:
        print ("*"+" "*(i-2)+"*")
    
    0 讨论(0)
提交回复
热议问题