简体   繁体   English

如何通过嵌套循环在python中创建一个*框?

[英]How to create a * box in python through nested loop?

I'm trying to create a *box through nested loop.我正在尝试通过嵌套循环创建一个 *box。 So How I can create box?那么我如何创建盒子?

I've tried to create a logic through some division, multiplication, subtraction, modules.I don't what is problem?我试图通过一些除法、乘法、减法、模块来创建逻辑。我不知道有什么问题?

for colm in range(1,5):
    print('* ',end='')
    for row in range(1,21):
        if (colm==1) or (colm==4) or (row==20  and colm==2) or (row==20 and colm==3) :
            print('*',end=' ')
    print('')

Output:输出:

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

I expected:我期望:

* * * * * * * * * * * * * * * * * * * * * 
*                                       *  <---- I want * here.
*                                       *  <---- I want * here.
* * * * * * * * * * * * * * * * * * * * *
rows = 5
cols = 23
for i in range(rows):
    print('*' + ('*' if i in (0,rows-1) else ' ') * (cols-2) + '*')

output输出

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

All rows start and end with an asterisk, and only the top and bottom fill, the middle rows all use spaces instead.所有行都以星号开始和结束,只有顶部和底部填充,中间的行都使用空格代替。

You need to fill in the missing space with something - your writing cursor is always after the last character, it doesn't magically move sideways.你需要用一些东西来填补缺失的空间——你的书写光标总是在最后一个字符之后,它不会神奇地横向移动。

Also, you can use replicating strings instead of the inner loop to get rid of if s:此外,您可以使用复制字符串而不是内部循环来摆脱if s:

print("*" * columns)
for row_no in range(rows-2): # -2 because we print top and bottom row differently
    print("*" + " "*(columns-2) + "*")

print("*" * columns)

Try this尝试这个

def print_rectangle(n, m) : 

    for i in range(1, n+1) : 
        for j in range(1, m+1) : 
            if (i == 1 or i == n or
                j == 1 or j == m) : 
                print("*", end="")             
            else : 
                print(" ", end="")             

        print() 


# Driver program for above function 
rows = 6 # Breadth of your Quadrilateral
columns = 20 #Length of your Quadrilateral
print_rectangle(rows, columns) 

The driver part of the program can be run-time based if you want, to do that you need to take integer input from the user.如果您愿意,程序的驱动程序部分可以基于run-time ,为此您需要从用户那里获取integer输入。

rows= int(input("Enter the breadth of your Quadrilateral"))

Now let's understand what happens when you call the print_rectangle(rows, columns)现在让我们了解调用print_rectangle(rows, columns)时会发生什么

The part that you need to understand is this你需要了解的部分是这个

for i in range(1, n+1) : 
            for j in range(1, m+1) : 
                if (i == 1 or i == n or
                    j == 1 or j == m) : 
                    print("*", end="")             
                else : 
                    print(" ", end="")

In simple language, if you observe the structure you want it to have * if it is the 1st column or the nth column OR if it the 1st row or the nth row用简单的语言,如果您观察到您希望它具有的结构*如果它是第1 列或第n 列OR如果它是第 1 行或第n 行

So the outer loop takes care of columns所以外循环处理列

 for i in range(1, n+1)

and the inner loop takes care of the number of rows.内部循环负责行数。

for j in range(1, m+1)

Now you must relate the variable i with row-number and j with column-number and then read the if clause .现在您必须将变量irow-number关联,将jcolumn-number关联,然后读取if clause You will find it says the same thing I explained above in terms of English words .你会发现它和我上面用英语单词解释的一样

If you need further clarification, I'll be happy to elaborate it for you if you let me know where you are having trouble.如果您需要进一步说明,如果您让我知道您遇到的问题,我很乐意为您详细说明。

You can use a simple algorithm to print box shapes您可以使用简单的算法来打印盒子形状

  1. Define the length and breadth of the box(rows and columns).定义框的长度和宽度(行和列)。
  2. Loop through rows and columns循环遍历行和列
  3. For row对于行

    • If the row item(i in our case is i==0 or i==row, then print '*' except print space .如果行项目(在我们的例子中是 i==0 或 i==row,则打印'*'除了 print space
    • this will print our left and right side of the box.这将打印盒子的左侧和右侧。
  4. For columns对于列

    • If the columns item(j in our case is j==0 or j==columns, then print '*' except print space .如果列 item(j 在我们的例子中是 j==0 或 j==columns,则打印'*'除了 print space
    • this will print our top and bottom side of box.这将打印我们盒子的顶部和底部。

Note: we used **end=''** to prevent newline after printing '*' or space注意:我们使用**end=''**来防止在打印'*'空格后换行


def box(rows, columns):
    for i in range(1, rows + 1):
        for j in range(1, columns + 1):
            if (i == 1 or i == rows or j == 1 or j == columns):
                print('*', end='  ')
            else:
                print(' ', end='  ')
        print()

square正方形

>>> box(10,10)
*  *  *  *  *  *  *  *  *  *  
*                          *  
*                          *  
*                          *  
*                          *  
*                          *  
*                          *  
*                          *  
*                          *  
*  *  *  *  *  *  *  *  *  *  

rectangle长方形

>>> box(10,5)
*  *  *  *  *  
*           *  
*           *  
*           *  
*           *  
*           *  
*           *  
*           *  
*           *  
*  *  *  *  *  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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