打印矩形星形图案,但基于坐标

  • 本文关键字:于坐标 坐标 打印 python
  • 更新时间 :
  • 英文 :


我正在学习python,但我正试图使星形矩形,但基于坐标。

rows = 5
cols = 5
for i in range(0, rows):
for j in range(0, cols):
print("*", end=" ")
print()

输出:

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

我想这样打印:

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

下面的程序应该会有所帮助:

rows = 5
cols = 5
for i in range(0, rows):
for j in range(0, cols):
if i==0 or i==rows-1 or j ==0 or j ==cols-1 or j == i:
print("*", end=" ")
else:
print(" ", end=" ")
print()

说明:
*有3种情况需要打印:

  1. 第一行或最后一行:检查为i==0 or i==rows-1
  2. 第一列或最后一列:检查为:j ==0 or j ==cols-1
  3. row number == column numberi==j.

最新更新