Python 绘制空心星号方块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16108446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:46:54  来源:igfitidea点击:

Drawing a hollow asterisk square

python

提问by Ergo

I'm trying to figure out how to turn my wholesquare into a hollow one. The few things I've tried so far haven't been very successful as I end up getting presented with a rather distorted triangle!

我想弄清楚如何把我的整个正方形变成一个空心的正方形。到目前为止,我尝试的几件事都不是很成功,因为我最终看到的是一个相当扭曲的三角形!

This is the code I have to form my square currently ..

这是我目前必须形成我的正方形的代码..

size = 5
for i in range(size):
    print ('*' * size)

When run, this is the result ..

运行时,这是结果..

*****
*****
*****
*****
*****

Do I need to run ifor whilestatements when sizeis greater than 3 to specify a condition?

当大于 3时是否需要运行ifwhile语句size来指定条件?

采纳答案by Reed Copsey

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

您可以打印出单个'*',然后是size-2空格,然后是单个'*'。这将为您提供“空心”部分。第一行和最后一行需要全长:

size = 5
inner_size = size - 2
print ('*' * size)
for i in range(inner_size):
    print ('*' + ' ' * inner_size + '*')
print ('*' * size)

回答by aldeb

I think this is what you want to do:

我认为这就是你想要做的:

m, n = 10, 10
for i in range(m):
    for j in range(n):
        print('*' if i in [0, n-1] or j in [0, m-1] else ' ', end='')
    print()

Output:

输出:

**********
*        *
*        *
*        *
*        *
*        *
*        *
*        *
*        *
**********

You can also draw a triangle this way:

你也可以这样画一个三角形:

m, n = 10, 10
for i in range(m):
    for j in range(n):
        print('*' if i in [j, m-1] or j == 0 else ' ', end='')
    print()

Output:

输出:

*         
**        
* *       
*  *      
*   *     
*    *    
*     *   
*      *  
*       * 
**********

回答by Mikk

Heres my example:

这是我的例子:

print("Enter width")
width = int(input())
print("Enter height")
height = int(input())

for i in range(height):
    if i in[0]:
        print("* "*(width))
    elif i in[(height-1)]:
        print("* "*(width))
    else:
        print("*"+"  "*(width-2)+" *")

input()

Output: Image link

输出:图像链接

Hope this helps everyone else, who wants to leave spaces between asteriks, while printing a rectangle and if there are any mistakes in my code let me know, because I'm a beginner myself.

希望这对其他人有所帮助,他们希望在打印矩形时在星号之间留出空格,如果我的代码中有任何错误,请告诉我,因为我自己也是初学者。

回答by Daku

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

回答by ahmedov

Here is my python code for drawing a square by entered size N.

这是我的python代码,用于按输入的大小N绘制正方形。

n = int(input())
print('*' * n)
for i in range(n-2):
    print ('*' + ' ' * (n-2) + '*')
print('*' * n)

Basically the first and the last print('*' * n)are drawing the top and the bottom lines and the for cycle prints the body.

基本上第一个和最后一个print('*' * n)绘制顶部和底部线条,for 循环打印主体。

Output example: N=3

输出示例:N=3

***
* *
***

Output example: N=5

输出示例:N=5

*****
*   *
*   *
*   *
*****

回答by user9093127

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:

输出:

*****
*   *
*   *
*   *
*****

回答by Vicky Kumar

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

这是我使用列大小和行大小输入绘制正方形的 python 3.6 代码:

column = int(input("Enter column size : "))
row = int(input("Enter row size : "))
print('* ' * column)
print(('* ' + "  " * (column-2)+ '*'+'\n')*(row -2) + ('* ' * column))

回答by Vicky Kumar

You can also draw a triangle using this simple way:

您还可以使用这种简单的方法绘制三角形:

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

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