Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用嵌套循环在Python中生成模式_Python_For Loop - Fatal编程技术网

使用嵌套循环在Python中生成模式

使用嵌套循环在Python中生成模式,python,for-loop,Python,For Loop,我试图制作一个简单的模式,允许用户决定行数和列数,例如: How many rows: 5 How many columns: 5 >>> ***** ***** ***** ***** ***** 所以我的代码是这样的: row = int(input('How many rows: ')) col = int(input('How may columns: ')) for row in range(row): for col in range(col):

我试图制作一个简单的模式,允许用户决定行数和列数,例如:

How many rows: 5
How many columns: 5
>>>
*****
*****
*****
*****
*****
所以我的代码是这样的:

row = int(input('How many rows: '))
col = int(input('How may columns: '))
for row in range(row):
    for col in range(col):
        print ('*', end='')
但结果是:

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

我意识到我为for循环的变量指定了与输入变量相同的变量名。一、 但是,我不理解该代码的逻辑。如果你们能给我解释一下流程图之类的东西,那就太好了。

这会循环
col
次,然后将
col
设置为
col-1

for col in range(col):
由于
range(col)
0
循环到
col-1
,并且由于循环完成后,循环变量在此时设置为循环退出时迭代的值

应该为循环索引使用不同的名称

row = int(input('How many rows: '))
col = int(input('How may columns: '))
for row_ in range(row):
    for col_ in range(col):
        print ('*', end='')

我现在看到了!谢谢你,伙计!