我正在学习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种情况需要打印:
- 第一行或最后一行:检查为
i==0 or i==rows-1
- 第一列或最后一列:检查为:
j ==0 or j ==cols-1
row number == column number
或i==j
.