编写一个使用 For 循环和 While 循环以及示例打印矩形星形图案的 Python 程序。
使用 For 循环打印矩形星形图案的 Python 程序
此 Python 程序允许用户输入绘制矩形的行数和列数。接下来,我们使用 Python 嵌套 For 循环来打印星形矩形。
# Python Program to Print Rectangle Star Pattern
rows = int(input("Please Enter the Total Number of Rows : "))
columns = int(input("Please Enter the Total Number of Columns : "))
print("Rectangle Star Pattern")
for i in range(rows):
for j in range(columns):
print('*', end = ' ')
print()

打印矩形星形示例 2 的 Python 程序
此 Python 程序 允许用户输入自己的字符。接下来,它会打印用户指定的字符矩形。
# Python Program to Print Rectangle Star Pattern
rows = int(input("Please Enter the Total Number of Rows : "))
columns = int(input("Please Enter the Total Number of Columns : "))
ch = input("Please Enter any Character : ")
print("Rectangle Star Pattern")
for i in range(rows):
for j in range(columns):
print('%c' %ch, end = ' ')
print()
Please Enter the Total Number of Rows : 15
Please Enter the Total Number of Columns : 18
Please Enter any Character : $
Rectangle Star Pattern
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
>>>
使用 While 循环打印星形矩形的 Python 程序
此 Python 星形矩形程序与第一个示例相同。但是,我们用 While 循环 替换了 For 循环。
# Python Program to Print Rectangle Star Pattern
rows = int(input("Please Enter the Total Number of Rows : "))
columns = int(input("Please Enter the Total Number of Columns : "))
print("Rectangle Star Pattern")
i = 0
while(i < rows):
j = 0
while(j < columns):
print('*', end = ' ')
j = j + 1
i = i + 1
print()
Please Enter the Total Number of Rows : 15
Please Enter the Total Number of Columns : 20
Rectangle Star Pattern
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
>>>